#2450 Actors in Javascript

SlimerDude Tue 1 Sep 2015

Hi, I was just wondering if anyone had given any thought as to how concurrent Actors may be implemented in Javascript?

I know there are promises and web workers but I've not yet looked into their suitability. Though I imagine the Future class and Actor.sendLater() to be the sticking points.

Jeremy Criquet Tue 1 Sep 2015

Any idea when Promises and Web Workers will make their way into most modern browsers? This is slick! I imagine porting Future and Actor.sendLater() into these won't be too hard at all.

andy Tue 1 Sep 2015

Web Workers used to require a physically separate script file, so was always a big complication. I haven't looked recently though.

SlimerDude Wed 2 Sep 2015

Web Workers can be inlined using Blobs - see Inline Workers. Stackoverflow also has a good question on it.

As for browser compatibility, see Can I use ... Web Workers and Can I use ... Promises.

tl;dr - Web Workers are supported, but IE 11 doesn't natively support Promises. Though there are many libraries that do the same job.

SlimerDude Wed 2 Sep 2015

Actually, the difficulty from using either WebWorkers (which seem a little heavy weight) or Promises is implementing Future.get() which waits for the actor's result - because Javascript is not very good at being synchronous!

New generator and yield features in Javascript ES6 could potentially be used (even with Web Workers - see Synchronous Web Workers).

async and await allow you to block until a Promise is resolved but is only available in Javascript ES7. It fits Actors perfectly and is what everyone in the Javascript community is waiting for. The long road to Async/Await in JavaScript is a really good article and mentions how a tiny library called co can be used to achieve the same await behaviour - see "Stage 3.5: Generators/Yields + co (ES6)".

If not ES6, then I'd be interested in a performant implementation optimised for synchronous calls like actor.send(msg).get(). Not too difficult if the msg is processed via setTimeout(f, 1) to make it run on the next event loop tick. get() would then first check if the msg has been processed, and if not, call the function straight away (synchronously).

andy Wed 9 Sep 2015

None of these area a great match for Actors - always felt we'd want a nice API (maybe in dom) that is specific to WebWorkers. Haven't given it a ton of thought though.

SlimerDude Thu 10 Sep 2015

I guess it depends on what the goal is. To make full use of what Javascript has to offer, then a bespoke JS only API is obviously the way to go.

I was coming at it from the angle of creating a cross platform library and to that end, yeilding promises ain't too bad. Though it would only work in a GUI driven environment where control of the UI thread could be handed back to the JS engine - so it may process any outstanding Actor messages. (But that's usually a given in JS programs.)

Login or Signup to reply.