DOMWorkerThreads
Contents
Web Worker API Proposal
API Proposal
[NoInterfaceObject] interface WorkerFactory { Worker createWorker(in DOMString scriptURL); }; interface Worker { void postMessage(in DOMString aMessage); // event handler attributes attribute EventListener onmessage; attribute EventListener onerror; attribute EventListener onload; attribute EventListener onunload; }; [NoInterfaceObject] interface WorkerGlobalScope { readonly attribute WorkerGlobalScope self; readonly attribute WorkerLocation location; readonly attribute boolean closing; void close(); // event handler attributes attribute EventListener onunload; // WorkerUtils void importScripts([Variadic] in DOMString urls); readonly attribute Storage localStorage; Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize); void showNotification(in DOMString title, in DOMString subtitle, in DOMString description); void showNotification(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick); }; [NoInterfaceObject] interface WorkerLocation { readonly attribute DOMString href; readonly attribute DOMString protocol; readonly attribute DOMString host; readonly attribute DOMString hostname; readonly attribute DOMString port; readonly attribute DOMString pathname; readonly attribute DOMString search; readonly attribute DOMString hash; };
Sample usage
This is a very suboptimal way of calculating a number in the Fibonacci sequence.
Main page:
worker = createWorker("f.js"); worker.onmessage = function(e) { alert("The 100th Fibonacci number is " + e.data); } worker.postMessage(100);
f.js:
parent.onmessage = function(e) { if (e.data <= 1) postMessage(e.data); w1 = createWorker("f.js"); w1.onmessage = handler; w1.postMessage(e.data - 1); w2 = createWorker("f.js"); w2.onmessage = handler; w2.postMessage(e.data - 2); } c = 0; sum = 0; function handler(e) { sum += parseInt(e.data); if (++c == 2) { postMessage(sum); } }
References
The only thing I've seen so far is the Google Gears WorkerPool API. We would certainly like to provide a API that would make migrating Gears code trivial.
If we want to support shared workers in the initial release of this spec, here are two proposals that will work with the above initial API.
Proposal 1
[NoInterfaceObject] interface WorkerFactory { ... Worker createSharedWorker(in DOMString name, in DOMString scriptURL); }; [NoInterfaceObject] interface WorkerParent { void postMessage(in DOMString aMessage); attribute EventListener onmessage; };
createSharedWorker
creates a new Worker
object that interacts with the same WorkerGlobalScope
as any previously existing Worker
s.
When a shared Worker
receives a message it can send data back to the sender using the Event.source
property which is a WorkerParent
.
Using WorkerGlobalScope.postMessage
and WorkerGlobalScope.onmessage
results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead).
Downsides with this proposal:
- There is no way to communicate back unless first communicated to.
- The first one to instantiate a shared worker get special treatment.
Proposal 2
[NoInterfaceObject] interface WorkerFactory { ... MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL); }; [NoInterfaceObject] interface WorkerGlobalScope { ... attribute EventListener onconnect; };
connectToSharedWorker
creates a two new MessagePort
which are entangled with each other. One of the two ports is returned, and the other is sent to the Worker
with the given name. If such a Worker
does not yet exist, one is created.
The Worker
receives the other MessagePort
through an onconnect
Event
fired on the WorkerGlobalScope
object.
- There is no way to get a reference to the shared
Worker
object itself. - Communication for shared workers is different from communication for non-shared ones.