W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: Synchronous postMessage for Workers?

From: Glenn Maynard <glenn@zewt.org>
Date: Thu, 17 Nov 2011 14:28:08 -0500
Message-ID: <CABirCh8s0Voz83rPno-sqG2dDigM7rdXOnqB8uwQwYsVBurAdA@mail.gmail.com>
To: Joshua Bell <jsbell@chromium.org>
Cc: public-webapps@w3.org
On Thu, Nov 17, 2011 at 1:37 PM, Joshua Bell <jsbell@chromium.org> wrote:

> Jonas and I were having an offline discussing regarding the synchronous
> Indexed Database API and noting how clean and straightforward it will allow
> Worker scripts to be. One general Worker issue we noted - independent of
> IDB - was that there are cases where Worker scripts may need to fetch data
> from the Window. This can be done today using bidirectional postMessage,
> but of course this requires the Worker to then be coded in now common
> asynchronous JavaScript fashion, with either a tangled mess of callbacks or
> some sort of Promises/Futures library, which removes some of the benefits
> of introducing sync APIs to Workers in the first place.
> Wouldn't it be lovely if the Worker script could simply make a synchronous
> call to fetch data from the Window?
> GTNW.prototype.end = function () {
>     var result = self.sendMessage({action: "prompt_user", prompt: "How
> about a nice game of chess?"});
>     if (result) { chess_game.begin(); }
> }

We discussed a very similar thing about a year ago; I've been meaning to
bring that up again, so this is probably as good a time as any.

The proposal is to allow polling a MessagePort, causing the first queued
message, if any, to be dispatched (or alternatively, to be returned).  This
could be easily extended to handle the above, by adding a "blocking
duration" parameter.

For example, working from Jonas's getMessageIfExists proposal:

  self.sendMessage({action: "prompt_user", prompt: "How about a nice game
of chess?"});
  var msg = messagePort.getMessageIfExists(5.0);
  if(msg && msg.data) { chess_game.begin(); }

Here, 5.0 means to block for five seconds (with a sentinel like -1 would
mean "block forever"), and the return value is the MessageEvent, returning
null if no message is received.

I preferred the "dispatch the first message" approach before, but your
blocking use case may make the "return the first message" approach better.

I think this is a fundamental missing piece to worker communication.  A
basic reason for having Workers in the first place is so you can write
linear code, instead of having to structure code to be able to return
regularly (often awkward and inconvenient), but currently in order to
receive messages in workers you still have to do that.

The use case that lead me to this initially was wanting to cancel
operations cleanly, without having to terminate and reinitialize the worker
entirely, eg. for input completion where processing restarts on each
keypress.  (That was an issue because the completion was initialized from
localStorage, making startup expensive; IndexedDB will help that particular
example, but this applies to any worker with nontrivial startup times.)

Glenn Maynard
Received on Thursday, 17 November 2011 19:28:38 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:36 UTC