Skip to content

Asynchronous versus Synchronous Communication

thomthom edited this page Apr 5, 2013 · 7 revisions

On PC the communication between javascript and ruby is synchronous. That means when you call a ruby method using window.location = 'skp:rubyMethod', the javascript will wait for the ruby method to complete before proceeding with the next javascript statement.

On OSX it just sends the command and continues with the javascript without waiting - making it impossible call sequential callbacks to ruby too quickly as they will override each other.

Calling from ruby to javascript using .execute_script is synchronous on both platforms. The method will wait for the javascript to finish before processing next line of ruby.

So you can do stuff like this:

dialog.execute_script('add(2,3);')
value = dialog.get_element_value('sketchupPump')

The add javascript function will here take both arguments and add them together and put the value into a hidden input field with the id 'hidden_input'.

function add(n1, n2)
{
  document.getElementById('sketchupPump').value = n1 + n2;
}

The input field in the HTML is something like this:

<input id="sketchupPump" type="hidden">

Further reading:

  • forums.sketchucation.com/viewtopic.php?f=180&t=13394
  • forums.sketchucation.com/viewtopic.php?f=180&t=22698
  • More thread on this topic exists over at SketchUcation.