DOMWorkerThreads: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 1: | Line 1: | ||
== Web Worker API Proposal == | == Web Worker API Proposal == | ||
== API Proposal == | == API Proposal == | ||
| Line 45: | Line 23: | ||
readonly attribute boolean closing; | readonly attribute boolean closing; | ||
void close(); | void close(); | ||
void postMessage(in DOMString aMessage); | |||
// event handler attributes | // event handler attributes | ||
attribute EventListener onmessage; | |||
attribute EventListener onunload; | attribute EventListener onunload; | ||
| Line 75: | Line 56: | ||
</code> | </code> | ||
</blockquote> | </blockquote> | ||
=== Sample usage === | === Sample usage === | ||
Main page: | |||
<blockquote> | <blockquote> | ||
<code> | <code> | ||
<pre> | <pre> | ||
worker = createWorker("f.js"); | |||
worker.onmessage = function(e) { | |||
alert("The 100th Fibonacci number is " + e.data); | |||
} | |||
worker.postMessage(100); | |||
</pre> | |||
</code> | |||
</blockquote> | |||
f.js: | |||
<blockquote> | |||
<code> | |||
<pre> | |||
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); | |||
} | |||
} | |||
</pre> | </pre> | ||
</code> | </code> | ||
</blockquote> | </blockquote> | ||
== References == | == References == | ||
The only thing I've seen so far is the [http://code.google.com/apis/gears/api_workerpool.html Google Gears WorkerPool API]. We would certainly like to provide a | The only thing I've seen so far is the [http://code.google.com/apis/gears/api_workerpool.html Google Gears WorkerPool API]. We would certainly like to provide a API that would make migrating Gears code trivial. | ||
Revision as of 22:52, 13 August 2008
Web Worker API Proposal
API Proposal
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(); void postMessage(in DOMString aMessage); // event handler attributes attribute EventListener onmessage; 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 WorkerFactory { Worker createWorker(in DOMString scriptURL); }; [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
Main page:
worker = createWorker("f.js"); worker.onmessage = function(e) { alert("The 100th Fibonacci number is " + e.data); } worker.postMessage(100);
f.js:
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.