W3C home > Mailing lists > Public > public-webapps@w3.org > January to March 2012

Re: Synchronous postMessage for Workers?

From: Glenn Maynard <glenn@zewt.org>
Date: Mon, 13 Feb 2012 15:25:54 -0600
Message-ID: <CABirCh_14srVC7W+GuN+ziGQLnM0_HWwMvro=L8qm0t5YfWQ2A@mail.gmail.com>
To: Ian Hickson <ian@hixie.ch>
Cc: Joshua Bell <jsbell@chromium.org>, Jonas Sicking <jonas@sicking.cc>, public-webapps@w3.org, David Levin <levin@chromium.org>
On Mon, Feb 13, 2012 at 3:10 PM, Glenn Maynard <glenn@zewt.org> wrote:

> alert("hello", finishedMessage);
> yieldUntil(finishedMessage);
> finishedFunc();
>


> which would send a message on port1 when it completes.  Waiting on
> multiple ports would be straightforward, eg. msg = yield([port1, port2,
> port3]), though MessageEvent would need a pointer back to the MessagePort
> so you can tell which it came from.
>

A use case tying these together is synchronously running two parallel XHR
requests.  Currently, if you're writing code in a Worker, your only option
to make two XHR calls simultaneously is to use the async API, which means
your own API must be async.  If you want to write a synchronous API, you
have to use the sync API, which means you can only make a single request at
a time.

With this mechanism, a hypothetical XHR API could allow this:

xhr1.open("GET", ..., port1a);
xhr1.send();
xhr2.open("GET", ..., port2a);
xhr2.send();

yieldUntil([port1b, port2b]); // wait for one of the requests to complete
yieldUntil([port1b, port2b]); // wait for the other request to complete

The same could be applied to use any combination of APIs concurrently, in a
function that behaves synchronously.  (These could have used the same
message channel.)

-- 
Glenn Maynard
Received on Monday, 13 February 2012 21:26:22 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:50 GMT