Is execute_script working really async?

Hi all,

I’ve a confusion about execute_script. In definition of this method written “The {#execute_script} method is used to execute a JavaScript string on the html dialog asynchronously.” when I try to iterate some calls in same process, it runs all them at the end of the operation. If it is async how can I await?

arr = [1,2,3,4,5]
arr.each do|i|
  # if execute_script async, how can I await it?
  html_dialog.execute_script("console.log(#{i})")
  sleep(1)
end

So what I was expecting to see in Browser console is logging them in order by waiting 1 sec.
But it happens instantly after 5 second. How can I achieve to call them instantly in process without waiting till process finished?

Thanks

At present, I don’t think that’s possible without some rather interesting code.

In your execute script string, you can have a callback to inform the Ruby side that the script has been processed.

I think the following will time your calls as you’d like…

arr = [1,2,3,4,5]
arr.each do|i|
  UI.start_timer(i) { html_dialog.execute_script "console.log(#{i})" }
end

The term ‘async’ is used, but what really happens is execute_script is processed after ruby code has ‘ended’. Your code hasn’t ‘ended’, as the sleep statements are executing. I need to double check that macOS works that way, but pretty sure it does… JFYI, I need more coffee.

Thanks for the answer @MSP_Greg,

I guess it is useless since it calls it at the end of the process, because sleep() function was basically used to see console.logging into console of HtmlDialog. I tried your way but nothing changed only it processed logging from end to start like console.log(5), console.log(4)...., console.log(1)

What I wanted in real case is basically inform UI while my ruby code processing to have update my progress bar.

JFYI, for years I’ve had a working progress bar on Windows. It’s messy, used Fibers, and I can’t get it to work on macOS. It also allows a user to cancel the operation.

It would certainly be helpful if synchronous operations between Ruby and HtmlDialogs were possible.

1 Like

Does this Fibers works smoothly in SketchUp? Or do we need to prepare our own C/C++ wrapper for this?
Becauce you may know even some built-in ruby modules doesn’t work well in SketchUp…

With current Windows versions, yes. But, it will slow things down.

Given that SU has used Ruby 2.7 for a few years, it’s likely that the next version may use Ruby 3.x. Whether Fibers can still be used for a progress bar when that happens, not sure.

For C++ progress bar code, see https://github.com/SketchUp/testup-2/tree/main/ruby-c-extension/sketchup-taskbarlist/TaskbarProgress/src. I’ve never tried it, and obviously, if one wants to call it from Ruby (as opposed to using Fiddle), it would need versions compiled for each Ruby version, similar to what pre-compiled gems do.

As mentioned, I couldn’t get the Fiber code to work with macOS. I’m not aware of any macOS issues with Fibers that exist with stand-alone Ruby, but it isn’t something I’ve really dug into.

So, given that the Ruby API behavior varies between macOS & Windows in SU, I’m not sure which is considered the ‘stable’ version.

Summing up, if you have the resources to create and maintain a C progress bar, that might be best.

1 Like

@MSP_Greg, thanks for the informations! If I will have some updates, I’ll post under this post.