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: Fri, 18 Nov 2011 12:56:08 -0500
Message-ID: <CABirCh8kgnCYn4Lu_S5nzrJuVmAVyu_9pATXo37GicMDmswgBw@mail.gmail.com>
To: David Levin <levin@chromium.org>
Cc: Jonas Sicking <jonas@sicking.cc>, Joshua Bell <jsbell@chromium.org>, public-webapps@w3.org
On Fri, Nov 18, 2011 at 12:18 PM, David Levin <levin@chromium.org> wrote:

> The primary use case is one in which messages sent to
> DedicatedWorkerGlobalScope are reserved for synchronous messaging.
>

Mostly, yes, or otherwise dealing with unrelated messages coming in while
you're waiting for a response.

It's possible to safely use async and sync message receipt on the same
channel.  For example,

onmessage = function(e)
{
    if(e.data.message == "pause")
    {
        msg = waitForMessage();
        // assert(msg.message == "continue");
    }
}

and in the main thread,

worker.postMessage({message = "pause"});
alert("The worker is currently paused and waiting to be continued");
worker.postMessage({message = "continue"});

Once you send "pause", events and timers in the worker will be deferred
until you resume it.  Since messages are delivered in order, this is
guaranteed to work.

(By the way, the above isn't why I prefer this API; I prefer it because it
avoids adding more messaging mechanisms and it creates a simpler API.  The
above is just an interesting way it might be used.)

-- 
Glenn Maynard
Received on Friday, 18 November 2011 17:56:45 GMT

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