DOMWorkerThreads: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 138: Line 138:
Downsides with this proposal:
Downsides with this proposal:
* There is no way to communicate back unless first communicated to.
* There is no way to communicate back unless first communicated to.
* The first one to instantiate a shared worker get special treatment.


=== Proposal 2 ===
=== Proposal 2 ===
Line 159: Line 160:
</blockquote>
</blockquote>


<code>connectToSharedWorker</code> creates a new <code>Worker</code> object if none exists for the given namethat interacts with the same <code>WorkerGlobalScope</code> as any previously existing <code>Worker</code>s.
<code>connectToSharedWorker</code> creates a two new <code>MessagePort</code> which are entangled with each other. One of the two ports is returned, and the other is sent to the <code>Worker</code> with the given name. If such a <code>Worker</code> does not yet exist, one is created.


When a shared <code>Worker</code> receives a message it can send data back to the sender using the <code>Event.source</code> property which is a <code>WorkerParent</code>.
The <code>Worker</code> receives the other <code>MessagePort</code> through an <code>onconnect</code> <code>Event</code> fired on the <code>WorkerGlobalScope</code> object.


Using <code>WorkerGlobalScope.postMessage</code> and <code>WorkerGlobalScope.onmessage</code> results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead).
* There is no way to get a reference to the shared <code>Worker</code> object itself.
 
* Communication for shared workers is different from communication for non-shared ones.
Downsides with this proposal:
* There is no way to communicate back unless first communicated to.
* The special treatment of the first context to open a shared worker.

Latest revision as of 16:58, 20 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 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 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

This is a very suboptimal way of calculating a number in the Fibonacci sequence.

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 1


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

[NoInterfaceObject] interface WorkerParent {
  void postMessage(in DOMString aMessage);
  attribute EventListener onmessage;
};

createSharedWorker creates a new Worker object that interacts with the same WorkerGlobalScope as any previously existing Workers.

When a shared Worker receives a message it can send data back to the sender using the Event.source property which is a WorkerParent.

Using WorkerGlobalScope.postMessage and WorkerGlobalScope.onmessage results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead).

Downsides with this proposal:

  • There is no way to communicate back unless first communicated to.
  • The first one to instantiate a shared worker get special treatment.

Proposal 2


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

[NoInterfaceObject] interface WorkerGlobalScope {
  ...
           attribute EventListener onconnect;  
};

connectToSharedWorker creates a two new MessagePort which are entangled with each other. One of the two ports is returned, and the other is sent to the Worker with the given name. If such a Worker does not yet exist, one is created.

The Worker receives the other MessagePort through an onconnect Event fired on the WorkerGlobalScope object.

  • There is no way to get a reference to the shared Worker object itself.
  • Communication for shared workers is different from communication for non-shared ones.