Any way to make scripts 'responsive' and not just freeze the page/execute the whole script?

I’d like to see the ability to make much more user-friendly UIs, but it’s quite limited when you hit the button and the page freezes while the whole script executes. First off, the page should be allowed to update (progress bar!), and also there should be an easy way to ask the user for continued input (IE, a custom browser could be created).

I’d do some looking at this myself outside of EteRNA specifically, but I honestly don’t know how it would work with how the interface might make the situation different from normal page scripting.

1 Like

The Eterna scripting environment _is _a Web page.  Web pages don’t ask for input from the user, they respond to input from the user.  So if you want an interactive Web page, you add event handlers to handle (i.e. execute code in response to) user input.

A trivial example: copy and paste the following script into the code window and click on Evaluate.

document.body.onclick = function() {outln("Click")};

The script will run and complete immediately.  But now each time you click on the page, a new line of “Click” will be added to the output window. 

Right, right. But how would I prevent the page from becoming completely unresponsive while executing? I’m not even sure what the cause is of that.

OK, I think I understand your question now.

Javascript is inherently single-threaded; that’s why there is no need for synchronization primitives in the language.  But that doesn’t mean you can’t get the end result you are looking for.

The standard method used to be to break up the long-running work into smaller pieces.  Each piece would

  1. Do some useful work, then
  2. Create a timer event that would kick off the next piece in the near future.
    This works because the Javascript interpreter can process any pending user input events before the timer kicks off and starts the next piece of the long computation.

In HTML 5, there is a concept of Web Workers.  A Web Worker is implemented essentially by starting up a new Javascript interpreter instance.   The main program can get results from the Web Worker by creating event handlers to respond to messages sent by the Web Worker.  

I did a quick check to see if the Eternascript environment supported Web Workers by running

outln (Worker)

and got back

function Worker() { [native code] }

so it appears, on the surface, to support them.

1 Like

Awesome, thank you!