1

Suppose you are building a robot simulator in HTML/JavaScript in which you have a robot that is controlled by the user's JavaScript code. You also want the user to be able to interact with the webpage so you send the user's code to a WebWorker where it is executed.

Now everything looks great until you come to a point when you need to control the robot based on the state of the webpage (i.e. is a robot touching some other object). You would like to send this information somehow to the WebWorker but that is not possible, because the user's code runs in infinite loop (using evilish eval) that blocks the worker completely.

So my question is: Is this the wrong approach? Is something I am trying to do technically impossible given the modern web browsers?

Thanks.

mr.Shu
  • 478
  • 5
  • 9
  • Try to use observer or mediator pattern, and attach events in the webworkers. – Filip Górny Jan 04 '14 at 21:46
  • Running the code in an infinite loop without listening for events is the wrong approach, in my opinion. – Pointy Jan 04 '14 at 21:49
  • @FilipGórny thanks, as a matter of a fact I actually use observer pattern but for events that should take place from inside the webpage. But if I understand it correctly this doesn't solve my problem -- I need some way of asynchronous communication with the WebWorker. – mr.Shu Jan 04 '14 at 21:55
  • @Pointy I certainly agree that it is not the best one given the asynchronousness of JavaScript. My problem is that it is quite inevitable for me to let users execute JavaScript code with infinite loops as that is a common structure of programs which are often found in robot software. That being said, do you think this is impossible to achieve? – mr.Shu Jan 04 '14 at 22:07
  • @mr.Shu if the code really is in an infinite loop, then it's impossible to communicate with the web worker. The JavaScript runtime cannot interrupt execution of a worker thread. The thread has to deliberately stop and wait for a message event. – Pointy Jan 04 '14 at 22:10

0 Answers0