DOMWorkerThreads: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
<code>
<code>
<pre>
<pre>
[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};
interface Worker
interface Worker
{
{
Line 15: Line 20:
           attribute EventListener onload;
           attribute EventListener onload;
           attribute EventListener onunload;
           attribute EventListener onunload;
};
[NoInterfaceObject] interface WorkerParent
{
  void postMessage(in DOMString aMessage);
  // event handler attributes
          attribute EventListener onmessage;
};
};


Line 21: Line 34:
   readonly attribute WorkerGlobalScope self;
   readonly attribute WorkerGlobalScope self;
   readonly attribute WorkerLocation location;
   readonly attribute WorkerLocation location;
  readonly attribute WorkerParent parent;
   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 36: Line 47:
   void showNotification(in DOMString title, in DOMString subtitle, in DOMString description);
   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);
   void showNotification(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
};
[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};
};


Line 76: Line 83:
<code>
<code>
<pre>
<pre>
onmessage = function(e) {
parent.onmessage = function(e) {
   if (e.data <= 1)
   if (e.data <= 1)
     postMessage(e.data);
     postMessage(e.data);
Line 105: Line 112:


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.
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.
== API Proposal For Shared Workers ==
If we want to support shared workers in the initial release of this spec, here are two proposals that will work with the above initial API.
=== Proposal 2 ===
<blockquote>
<code>
<pre>
[NoInterfaceObject] interface WorkerFactory {
  ...
  Worker createSharedWorker(in DOMString name, in DOMString scriptURL);
};
</pre>
</code>
</blockquote>
<code>createSharedWorker</code> creates a new <code>Worker</code> object that interacts with the same <code>WorkerGlobalScope</code> as any previously existing <code>Worker</code>s. When <code>postMessage</code> is called from within a shared <code>WorkerGlobalScope</code> all connected <code>Worker</code> objects will received that message through their <code>onmessage</code> property.
The

Revision as of 01:14, 14 August 2008

Web Worker API Proposal

API Proposal


[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};

interface Worker
{
  void postMessage(in DOMString aMessage);

  // event handler attributes
           attribute EventListener onmessage;
           attribute EventListener onerror;
           attribute EventListener onload;
           attribute EventListener onunload;
};

[NoInterfaceObject] interface WorkerParent
{
  void postMessage(in DOMString aMessage);

  // event handler attributes
           attribute EventListener onmessage;
};

[NoInterfaceObject] interface WorkerGlobalScope
{
  readonly attribute WorkerGlobalScope self;
  readonly attribute WorkerLocation location;
  readonly attribute WorkerParent parent;
  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 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:

parent.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.

API Proposal For Shared Workers

If we want to support shared workers in the initial release of this spec, here are two proposals that will work with the above initial API.

Proposal 2


[NoInterfaceObject] interface WorkerFactory {
  ...
  Worker createSharedWorker(in DOMString name, in DOMString scriptURL);
};

createSharedWorker creates a new Worker object that interacts with the same WorkerGlobalScope as any previously existing Workers. When postMessage is called from within a shared WorkerGlobalScope all connected Worker objects will received that message through their onmessage property.

The