[Bug 15007] Add an API to queue a task

https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007

--- Comment #14 from Glenn Maynard <glenn@zewt.org> 2011-12-02 20:24:29 UTC ---
(In reply to comment #10)
> postMessage wasn't intended for that. Setting a listener for message on window
> means that other windows can invoke the listener, so the boilerplate needs to
> prevent that. Also, one might want to use postMessage for its intended purpose,
> too, which means both the zero-timeout boilerplate *and* the normal postMessage
> use need to filter the incoming messages.

However, I notice you can use MessageChannel for this purpose, without any of
these problems.

var queueTask = function(task)
{
    var mc = new MessageChannel();
    mc.port1.onmessage = task;
    mc.port2.postMessage(null);
}

queueTask(function() { console.log("test"); });

In Chrome, this usually results in about a 10ms delay, but that's an
implementation detail (you get that from most async callbacks).  It's not
mandated by the spec, like the setTimeout minimum delay.

This isn't exactly the same as "queue a task", since the port message queue is
separate from the task queue, so the function will be called in a different
order relative to other tasks.  I doubt that's a problem.

I think I like this better than adding a new method, and it seems like a fairly
natural use of MessageChannel.

-- 
Configure bugmail: https://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Friday, 2 December 2011 20:24:35 UTC