Sketchup request does not work when called at start but otherwise

In the plugin , on start , if a request is called then response is not returned.
But when the button is clicked and same request is executed , correct response is returned.

PluginName: testsentio
test.zip (3.3 KB)

you should look at ‘lazy loading’ all of your files, so they don’t load on startup…

you can add an over-ride for debugging, and comment out for deployment…

## Tidied up code below in latter post...

john

Sorry i do not understand how this will help because flow seems to be same.
Also I have toolbar in the plugin so that should load on startup.

the flow IS different, as it’s deliberately withholding the loading…

you should add cmd’s [so you can add a shortcut key] then use the cmd for menu and toolbar…

I tidied up the first post’s ruby…

you need to change ENV['LOGNAME'] == 'johns_iMac' to your computer…

module TestSolutionario
  module TestSentio
    module_function

    DEBUG ||= true if ENV['LOGNAME'] == 'johns_iMac'

    def lazy_load
      require 'sketchup.rb'
      require 'net/http'
      require 'json'
      require 'pathname'

      Sketchup.require(File.join(File.dirname(__FILE__), "config"))
      Sketchup.require(File.join(File.dirname(__FILE__), "sentio_logger"))
      Sketchup.require(File.join(File.dirname(__FILE__), "csrf_request"))
      Sketchup.require(File.join(File.dirname(__FILE__), "pluginreloader"))
    end

    def init
      SKETCHUP_CONSOLE.show if DEBUG

      csrfReq = CSRFRequest.new
      csrfReq.get_token { |t| puts "#{t}"}
    end

    unless defined? MENU

			# add a cmd for menu, toolbar or shortcut item...
			title = 'TestSentioVR'
			cmd   = UI::Command.new(title) do
				lazy_load
				init
			end

			cmd.tooltip = cmd.status_bar_text = 'cmd_message...'

			cmd.large_icon = cmd.small_icon = File.join(__dir__, 'Resources', 'images', 'name')

			MENU ||=  UI.menu('Plugins', title)
                       menu = MENU
			# menu item
			menu.add_item(cmd)
			# create toolbar
			TBAR ||= UI::Toolbar.new(title)
                        tb = TBAR
			# toolbar item
		        tb.add_item(cmd)
			# showing the toolbar
			tb.get_last_state  == TB_NEVER_SHOWN ? tb.show : tb.restore

    end

    if DEBUG
      lazy_load
      init
    end

  end
end

EDIT: made some minor changes to constants…

john

1 Like

Thank you @john_drivenupthewall for explaining. I will try out these changes.

ENV["LOGNAME"] is Mac only. Windows uses ENV["COMPUTERNAME"]

1 Like

One more thing if on the startup I want to request some information from server.
Can’t I do that like version check or login.

on SU startup there are could be lots of request coming from many extensions…

this can lead to missed conections, loading delays or even crashes…

ALL extensions should do their checks only when the user wants to run them, not on SU startup…

on Extension startup [ using ‘lazy loading’ ], when the user clicks any of your buttons, you have a clear path for your checks…

they’ll happen quicker and have less potential issues…

if they fail, you can let the user know without the risk of your message being swallowed…

john

I tend to wrap everything in a proxy object, so that references can get passed to other objects, but initialization only occurs when a method is really called.

See https://medium.com/@dljerome/design-patterns-in-ruby-proxy-48a379a3b8d3 for more info on the proxy design pattern.