DOMWorkerThreads current: Difference between revisions
Jump to navigation
Jump to search
Bent.mozilla (talk | contribs) No edit summary |
Bent.mozilla (talk | contribs) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
This page describes the API used in the Firefox 3.1 Beta 2 release. We expect few changes before final release, and nothing that would break compatibility with what we currently implement. | |||
Please do file bugs if you find any! | |||
== Sample usage == | == Sample usage == | ||
fibonacci.html: | |||
<blockquote> | <blockquote> | ||
<code> | <code> | ||
<pre> | <pre> | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
<html> | <html> | ||
<title> | <title>Test threads fibonacci</title> | ||
<body> | <body> | ||
<div id="result"></div> | |||
<script language="javascript"> | <script language="javascript"> | ||
var worker = new Worker("fibonacci.js"); | |||
function | worker.onmessage = function(event) { | ||
document.getElementById("result").textContent = event.data; | |||
}; | |||
worker.onerror = function(event) { | |||
dump("Worker error: " + event.data + "\n"); | |||
}; | }; | ||
worker.postMessage(10); | |||
</script> | </script> | ||
</body> | </body> | ||
</html> | </html> | ||
</pre> | |||
</code> | |||
</blockquote> | |||
fibonacci.js: | |||
<blockquote> | |||
<code> | |||
<pre> | |||
var results = []; | |||
function resultReceiver(event) { | |||
results.push(parseInt(event.data)); | |||
if (results.length == 2) { | |||
postMessage(results[0] + results[1]); | |||
} | |||
} | |||
onmessage = function(event) { | |||
var n = parseInt(event.data); | |||
if (n == 0 || n == 1) { | |||
postMessage(n); | |||
return; | |||
} | |||
for (var i = 1; i <= 2; i++) { | |||
var worker = new Worker("fibonacci.js"); | |||
worker.onmessage = resultReceiver; | |||
worker.postMessage(n - i); | |||
} | |||
} | |||
</pre> | </pre> | ||
</code> | </code> | ||
</blockquote> | </blockquote> | ||
== | == Full API == | ||
This API | This API shouldn't change between Beta 2 and final with the exception of adding a few properties, so code written to the following spec should continue to work. | ||
=== The worker object === | |||
Workers are created as follows: | |||
<blockquote> | <blockquote> | ||
<code> | <code> | ||
<pre> | <pre> | ||
var worker = new Worker("workerScript.js"); | |||
</pre> | |||
</code> | |||
</blockquote> | |||
interface | Once created, the worker object has the following interface: | ||
<blockquote> | |||
interface | <code> | ||
<pre> | |||
interface Worker : DOMEventTarget | |||
{ | { | ||
/** | /** | ||
* | * Event listener that will be called whenever a MessageEvent bubbles through | ||
* worker | * the worker with type 'error'. The error message will be stored in the | ||
* 'data' member of the event. | |||
* | |||
*/ | */ | ||
attribute EventListener onerror; | |||
/** | /** | ||
* | * Event listener that will be called whenever a MesageEvent bubbles through | ||
* the worker with type 'message'. The message will be stored in the 'data' | |||
* member of the event. | |||
* | |||
* | |||
*/ | */ | ||
attribute EventListener onmessage; | |||
/** | /** | ||
* Sends a | * Sends 'aMessage' as the 'data' member of a MessageEvent targeted at the | ||
* | * worker's inner scope. | ||
*/ | */ | ||
void postMessage(in DOMString aMessage); | void postMessage(in DOMString aMessage); | ||
}; | }; | ||
</pre> | |||
</code> | |||
</blockquote> | |||
=== The worker scope === | |||
The global object for the scope in which a worker's script runs has the following interface: | |||
<blockquote> | |||
<code> | |||
<pre> | |||
interface WorkerScope : DOMEventTarget | |||
{ | |||
/** | /** | ||
* | * Gets WorkerScope object itself. | ||
*/ | */ | ||
readonly attribute WorkerScope self; | |||
/** | /** | ||
* | * Event listener that will be called whenever a MesageEvent bubbles through | ||
* the worker's inner scope with type 'message'. The message will be stored | |||
* in the 'data' member of the event. | |||
*/ | */ | ||
attribute EventListener onmessage; | |||
/** | /** | ||
* | * Sends 'aMessage' as the 'data' member of a MessageEvent targeted at the | ||
* scope's parent worker object. | |||
*/ | */ | ||
void postMessage(in DOMString aMessage); | |||
/** | /** | ||
* | * Loads the scripts referenced by the given urls. Begins downloading all | ||
* | * scripts simultaneously but ensures that they are executed in the order | ||
* | * given. | ||
*/ | */ | ||
void importScripts([Variadic] in DOMString aURLs); | |||
/** | /** | ||
* | * The following constructors are available just as on the main thread: | ||
* | |||
* 1. Worker(in DOMString aURL); | |||
* var worker = new Worker("scriptUrl.js"); | |||
* | |||
* 2. XMLHttpRequest(); | |||
* var xhr = new XMLHttpRequest(); | |||
* | * | ||
* | * Note: The XHR objects created in a worker script behave identically to | ||
* | * the XHR objects created on the main thread except that the | ||
* 'responseXML' and 'channel' attributes always return null (see | |||
* nsIXMLHttpRequest.idl). | |||
*/ | */ | ||
/** | /** | ||
* See nsIDOMJSWindow.idl | * See nsIDOMJSWindow.idl for info on the following methods. | ||
*/ | */ | ||
void dump(in DOMString | void dump(in DOMString aMessage); | ||
long setTimeout(); | |||
long setInterval(); | |||
void clearTimeout(); | |||
void clearInterval(); | |||
long setTimeout( | |||
long setInterval( | |||
void clearTimeout( | |||
void clearInterval( | |||
}; | }; | ||
</pre> | </pre> | ||
</code> | </code> | ||
</blockquote> | </blockquote> | ||
Latest revision as of 19:53, 6 November 2008
This page describes the API used in the Firefox 3.1 Beta 2 release. We expect few changes before final release, and nothing that would break compatibility with what we currently implement.
Please do file bugs if you find any!
Sample usage
fibonacci.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <title>Test threads fibonacci</title> <body> <div id="result"></div> <script language="javascript"> var worker = new Worker("fibonacci.js"); worker.onmessage = function(event) { document.getElementById("result").textContent = event.data; }; worker.onerror = function(event) { dump("Worker error: " + event.data + "\n"); }; worker.postMessage(10); </script> </body> </html>
fibonacci.js:
var results = []; function resultReceiver(event) { results.push(parseInt(event.data)); if (results.length == 2) { postMessage(results[0] + results[1]); } } onmessage = function(event) { var n = parseInt(event.data); if (n == 0 || n == 1) { postMessage(n); return; } for (var i = 1; i <= 2; i++) { var worker = new Worker("fibonacci.js"); worker.onmessage = resultReceiver; worker.postMessage(n - i); } }
Full API
This API shouldn't change between Beta 2 and final with the exception of adding a few properties, so code written to the following spec should continue to work.
The worker object
Workers are created as follows:
var worker = new Worker("workerScript.js");
Once created, the worker object has the following interface:
interface Worker : DOMEventTarget { /** * Event listener that will be called whenever a MessageEvent bubbles through * the worker with type 'error'. The error message will be stored in the * 'data' member of the event. */ attribute EventListener onerror; /** * Event listener that will be called whenever a MesageEvent bubbles through * the worker with type 'message'. The message will be stored in the 'data' * member of the event. */ attribute EventListener onmessage; /** * Sends 'aMessage' as the 'data' member of a MessageEvent targeted at the * worker's inner scope. */ void postMessage(in DOMString aMessage); };
The worker scope
The global object for the scope in which a worker's script runs has the following interface:
interface WorkerScope : DOMEventTarget { /** * Gets WorkerScope object itself. */ readonly attribute WorkerScope self; /** * Event listener that will be called whenever a MesageEvent bubbles through * the worker's inner scope with type 'message'. The message will be stored * in the 'data' member of the event. */ attribute EventListener onmessage; /** * Sends 'aMessage' as the 'data' member of a MessageEvent targeted at the * scope's parent worker object. */ void postMessage(in DOMString aMessage); /** * Loads the scripts referenced by the given urls. Begins downloading all * scripts simultaneously but ensures that they are executed in the order * given. */ void importScripts([Variadic] in DOMString aURLs); /** * The following constructors are available just as on the main thread: * * 1. Worker(in DOMString aURL); * var worker = new Worker("scriptUrl.js"); * * 2. XMLHttpRequest(); * var xhr = new XMLHttpRequest(); * * Note: The XHR objects created in a worker script behave identically to * the XHR objects created on the main thread except that the * 'responseXML' and 'channel' attributes always return null (see * nsIXMLHttpRequest.idl). */ /** * See nsIDOMJSWindow.idl for info on the following methods. */ void dump(in DOMString aMessage); long setTimeout(); long setInterval(); void clearTimeout(); void clearInterval(); };