DOMWorkerThreads
DOM Worker Threads API Proposal
Abstract
This is an API proposal for DOM Worker Threads.
At this time this document is not complete. While we invite discussion on this topic, this is not a stable API proposal, and Mozilla won't implement anything based on it till it becomes stable.
Look for a post to mozilla.dev.platform when this proposal is ready to be discussed.
Introduction
Use Cases
Terminology
A worker is an execution context for JavaScript that runs off of the main UI thread. The global scope for a worker is almost empty - familiar objects such as window and document are not available, nor is the Mozilla-specific Components object.
A worker pool is a collection of related workers. Workers within a pool can communicate with each other freely, but workers cannot communicate with workers from another pool.
Conformance
Dependencies
Specification Proposal
#include "nsISupports.idl" interface nsIDOMWorkerThread; interface nsIScriptError; 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 WorkerFactory { Worker createWorker(in DOMString scriptURL); Worker createSharedWorker(in DOMString name, 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; };
The worker's scope
The worker's scope is far more limited. See WorkerGlobalScope above for details.
Sample usage
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <title>Test threads</title> <body> <script language="javascript"> var scriptToRun = 'function messageListener(message, source) { ' + ' dump("Worker: " + message + "\\n"); ' + '} ' + 'for (var i = 0; i < 10000000; i++) { ' + ' /* do something */ ' + '} '+ 'postMessageToPool("Done!"); '; var wp = navigator.newWorkerPool(); wp.messageListener = function(message, source) { dump("Pool: " + message + "\n"); }; wp.errorListener = function(error, source) { dump("Pool: error = '" + error + "'\n"); } var threads = [] for (var i = 0; i < 10; i++) { var thread = wp.createWorker(scriptToRun); thread.postMessage("hello"); threads.push(thread); } </script> </body> </html>
Not In This Specification
References
The only thing I've seen so far is the Google Gears WorkerPool API. We would certainly like to provide a compatibility API that would make migrating Gears code trivial.