DOMWorkerThreads: Difference between revisions
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
}; | }; | ||
interface Worker | interface Worker { | ||
{ | |||
void postMessage(in DOMString aMessage); | void postMessage(in DOMString aMessage); | ||
Line 22: | Line 21: | ||
}; | }; | ||
[NoInterfaceObject] interface WorkerGlobalScope | [NoInterfaceObject] interface WorkerGlobalScope | ||
Line 34: | Line 26: | ||
readonly attribute WorkerGlobalScope self; | readonly attribute WorkerGlobalScope self; | ||
readonly attribute WorkerLocation location; | readonly attribute WorkerLocation location; | ||
readonly attribute boolean closing; | readonly attribute boolean closing; | ||
void close(); | void close(); | ||
Line 65: | Line 56: | ||
=== Sample usage === | === Sample usage === | ||
This is a '''very''' suboptimal way of calculating a number in the Fibonacci sequence. | |||
Main page: | Main page: | ||
Line 116: | Line 109: | ||
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. | 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 === | |||
<blockquote> | |||
<code> | |||
<pre> | |||
[NoInterfaceObject] interface WorkerFactory { | |||
... | |||
Worker createSharedWorker(in DOMString name, in DOMString scriptURL); | |||
}; | |||
[NoInterfaceObject] interface WorkerParent { | |||
void postMessage(in DOMString aMessage); | |||
attribute EventListener onmessage; | |||
}; | |||
</pre> | |||
</code> | |||
</blockquote> | |||
<code>createSharedWorker</code> creates a new <code>Worker</code> object that interacts with the same <code>WorkerGlobalScope</code> as any previously existing <code>Worker</code>s. | |||
When a shared <code>Worker</code> receives a message it can send data back to the sender using the <code>Event.source</code> property which is a <code>WorkerParent</code>. | |||
Using <code>WorkerGlobalScope.postMessage</code> and <code>WorkerGlobalScope.onmessage</code> 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. | |||
=== Proposal 2 === | === Proposal 2 === | ||
Line 125: | Line 147: | ||
[NoInterfaceObject] interface WorkerFactory { | [NoInterfaceObject] interface WorkerFactory { | ||
... | ... | ||
MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL); | |||
}; | |||
[NoInterfaceObject] interface WorkerGlobalScope { | |||
... | |||
attribute EventListener onconnect; | |||
}; | }; | ||
Line 132: | Line 159: | ||
</blockquote> | </blockquote> | ||
<code> | <code>connectToSharedWorker</code> creates a new <code>Worker</code> object if none exists for the given namethat interacts with the same <code>WorkerGlobalScope</code> as any previously existing <code>Worker</code>s. | ||
When a shared <code>Worker</code> receives a message it can send data back to the sender using the <code>Event.source</code> property which is a <code>WorkerParent</code>. | |||
Using <code>WorkerGlobalScope.postMessage</code> and <code>WorkerGlobalScope.onmessage</code> results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead). | |||
The | Downsides with this proposal: | ||
* There is no way to communicate back unless first communicated to. | |||
* The special treatment of the first context to open a shared worker. |
Revision as of 16:33, 15 August 2008
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.
Proposal 2
[NoInterfaceObject] interface WorkerFactory { ... MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL); }; [NoInterfaceObject] interface WorkerGlobalScope { ... attribute EventListener onconnect; };
connectToSharedWorker
creates a new Worker
object if none exists for the given namethat 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 special treatment of the first context to open a shared worker.