Re: Synchronous postMessage for Workers?

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 UTC