DOMWorkerThreads: Difference between revisions

no edit summary
No edit summary
Line 1: Line 1:
== Web Worker API Proposal ==
== Web Worker 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 <code>window</code> and <code>document</code> are not available, nor is the Mozilla-specific <code>Components</code> 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 ==


== 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>
=== The worker's scope ===
The worker's scope is far more limited. See <code>WorkerGlobalScope</code> above for details.


=== Sample usage ===
=== Sample usage ===


Main page:
<blockquote>
<blockquote>
<code>
<code>
<pre>
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
worker = createWorker("f.js");
<html>
worker.onmessage = function(e) {
  <title>Test threads</title>
  alert("The 100th Fibonacci number is " + e.data);
  <body>
}
  <script language="javascript">
worker.postMessage(100);
    var scriptToRun = 'function messageListener(message, source) { ' +
</pre>
                      '  dump("Worker: " + message + "\\n"); ' +
</code>
                      '} ' +
</blockquote>
                      'for (var i = 0; i < 10000000; i++) { ' +
                      ' /* do something */ ' +
                      '} '+
                      'postMessageToPool("Done!"); ';


    var wp = navigator.newWorkerPool();
f.js:
    wp.messageListener = function(message, source) {
<blockquote>
      dump("Pool: " + message + "\n");
<code>
    };
<pre>
    wp.errorListener = function(error, source) {
onmessage = function(e) {
      dump("Pool: error = '" + error + "'\n");
  if (e.data <= 1)
    }
    postMessage(e.data);


    var threads = []
  w1 = createWorker("f.js");
    for (var i = 0; i < 10; i++) {
  w1.onmessage = handler;
      var thread = wp.createWorker(scriptToRun);
  w1.postMessage(e.data - 1);
      thread.postMessage("hello");
 
      threads.push(thread);
  w2 = createWorker("f.js");
    }
  w2.onmessage = handler;
  w2.postMessage(e.data - 2);
}


  </script>
c = 0;
  </body>
sum = 0;
</html>


function handler(e) {
  sum += parseInt(e.data);
  if (++c == 2) {
    postMessage(sum);
  }
}
</pre>
</pre>
</code>
</code>
</blockquote>
</blockquote>
== Not In This Specification ==


== 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 compatibility API that would make migrating Gears code trivial.
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.
 
== Acknowledgements ==
Confirmed users
716

edits