DOMWorkerThreads current: Difference between revisions
Jump to navigation
Jump to search
Bent.mozilla (talk | contribs) |
Bent.mozilla (talk | contribs) No edit summary |
||
Line 212: | Line 212: | ||
var thread = wp.createWorker("" + scriptToRun + "scriptToRun();"); | var thread = wp.createWorker("" + scriptToRun + "scriptToRun();"); | ||
thread.postMessage( | thread.postMessage(35); | ||
</script> | </script> |
Revision as of 00:18, 5 September 2008
Here's what we have in Firefox 3.1 Alpha 1:
#include "nsISupports.idl" interface nsIDOMWorkerThread; interface nsIScriptError; [scriptable, function, uuid(e50ca05d-1381-4abb-a021-02eb720cfc38)] interface nsIDOMWorkerMessageListener : nsISupports { /** * An nsIDOMWorkerThread receives the onMessage callback when another * worker posts a message to it. * * @param aMessage (in DOMString) * The message sent from another worker. * @param aSource (in nsIDOMWorkerThread) * The worker that sent the message. Useful for a quick response. */ void onMessage(in DOMString aMessage, in nsIDOMWorkerThread aSource); }; [scriptable, function, uuid(9df8422e-25dd-43f4-b9b9-709f9e074647)] interface nsIDOMWorkerErrorListener : nsISupports { /** * An nsIDOMWorkerPool receives the onError callback when one of its child * workers has a parse error or an unhandled exception. * * @param aMessage (in nsIScriptError) * Details about the error that occurred. See nsIScriptError. * @param aSource (in nsIDOMWorkerThread) * The worker that sent the message. Depending on the specific error in * question it may not be possible to use this object (in the case of a * parse error, for instance, aSource will be unusable). */ void onError(in nsIScriptError aError, in nsIDOMWorkerThread aSource); }; [scriptable, uuid(6f19f3ff-2aaa-4504-9b71-dca3c191efed)] interface nsIDOMWorkerThread : nsISupports { /** * Sends a message to the worker. * * @param aMessage (in DOMString) * The message to send. */ void postMessage(in DOMString aMessage); }; [scriptable, uuid(45312e93-8a3e-4493-9bd9-272a6c23a16c)] interface nsIDOMWorkerPool : nsIDOMWorkerThread { /** * The nsIDOMWorkerMessageListener which handles messages for this pool. * * Developers should set this attribute to a proper object before another * worker begins sending messages to ensure that all messages are received. */ attribute nsIDOMWorkerMessageListener messageListener; /** * The nsIDOMWorkerErrorListener which handles errors in child threads. * * Developers should set this attribute to a proper object before calling * createWorker in order to catch parse errors as well as runtime exceptions. */ attribute nsIDOMWorkerErrorListener errorListener; /** * Create a new worker object by evaluating the given script. * * @param aSourceScript (in DOMString) * The script to compile. See below for details on the scope in which * the script will run. */ nsIDOMWorkerThread createWorker(in DOMString aScriptText); }; interface nsIDOMWorkerThreadContext { /** * The worker object that created this scope. */ readonly attribute nsIDOMWorkerThread thisThread; }; interface nsIDOMWorkerGlobalScope { /** * The thread context. */ readonly attribute nsIDOMWorkerThreadContext threadContext; /** * The nsIDOMWorkerMessageListener which handles messages for this worker. * * Developers should set this attribute to a proper object before another * worker begins sending messages to ensure that all messages are received. */ attribute nsIDOMWorkerMessageListener messageListener; /** * Sends a message to the pool that created the worker. * * @param aMessage (in DOMString) * The message to send. This may be a string or an object that can be * serialized via JSON (see http://developer.mozilla.org/en/docs/JSON). */ void postMessageToPool(in DOMString aMessage); /** * See nsIDOMJSWindow.idl */ void dump(in DOMString str); /** * See nsIDOMJSWindow.idl */ long setTimeout(/* in JSObject aFunctionOrString, */ /* in JSObject aMilisecondDelay, */ /* [optional] in aArgsToFunctionOrString */); /** * See nsIDOMJSWindow.idl */ long setInterval(/* in JSObject aFunctionOrString, */ /* in JSObject aMilisecondDelay, */ /* [optional] in aArgsToFunctionOrString */); /** * See nsIDOMJSWindow.idl */ void clearTimeout(/* in JSObject aTimeoutId */); /** * See nsIDOMJSWindow.idl */ void clearInterval(/* in JSObject aIntervalId */); };
The worker pool's scope
The scope for the worker pool is the standard DOM scope. All DOM objects such as window
and document
are available, as well as functions such as alert()
and dump()
.
The worker's scope
The worker's scope is far more limited. See nsIDOMWorkerGlobalScope
above for details.
Sample usage
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <title>Fibonacci from a worker thread</title> <body> <div id="start-div"></div> <p><div id="result-div">Result from worker: (pending)</div></p> <div id="end-div"></div> <script language="javascript"> var startdiv = document.getElementById("start-div"); var resultdiv = document.getElementById("result-div"); var enddiv = document.getElementById("end-div"); startdiv.innerHTML = "Started: " + new Date(); function scriptToRun(){ // Hook us up to receive messages from the pool this.messageListener = function(message, source) { var num = parseInt(message); if (isNaN(num)) { throw "Pool sent a value we couldn't interpret!"; } postMessageToPool(fibonacci(num)); }; // Worst implementation of fibonacci *ever*! function fibonacci(n) { if(n == 0) return 0; if(n == 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } } var wp = navigator.newWorkerPool(); wp.messageListener = function(message, source) { resultdiv.innerHTML = "Result from worker: " + message; enddiv.innerHTML = "Finished: " + new Date(); }; wp.errorListener = function(error, source) { alert("Worker had an error: " + error); } var thread = wp.createWorker("" + scriptToRun + "scriptToRun();"); thread.postMessage(35); </script> </body> </html>