DOMWorkerThreads: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:
<code>
<code>
<pre>
<pre>
interface Worker
 
{
[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};
 
interface Worker {
   void postMessage(in DOMString aMessage);
   void postMessage(in DOMString aMessage);


Line 16: Line 20:
           attribute EventListener onunload;
           attribute EventListener onunload;
};
};


[NoInterfaceObject] interface WorkerGlobalScope
[NoInterfaceObject] interface WorkerGlobalScope
Line 23: Line 28:
   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 38:
   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 58: Line 56:


=== Sample usage ===
=== Sample usage ===
This is a '''very''' suboptimal way of calculating a number in the Fibonacci sequence.


Main page:
Main page:
Line 76: Line 76:
<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 105:


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 1 ===
<blockquote>
<code>
<pre>
[NoInterfaceObject] interface WorkerFactory {
  ...
  Worker createSharedWorker(in DOMString name, in DOMString scriptURL);
};
[NoInterfaceObject] interface WorkerParent {
  void postMessage(in DOMString aMessage);
  attribute EventListener onmessage;
};
</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 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>.
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).
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 ===
<blockquote>
<code>
<pre>
[NoInterfaceObject] interface WorkerFactory {
  ...
  MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL);
};
[NoInterfaceObject] interface WorkerGlobalScope {
  ...
          attribute EventListener onconnect; 
};
</pre>
</code>
</blockquote>
<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.
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.
* 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.
Confirmed users
716

edits