Confirmed users
716
edits
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
}; | }; | ||
interface Worker | interface Worker { | ||
{ | |||
void postMessage(in DOMString aMessage); | void postMessage(in DOMString aMessage); | ||
Line 22: | Line 21: | ||
}; | }; | ||
[NoInterfaceObject] interface WorkerGlobalScope | [NoInterfaceObject] interface WorkerGlobalScope | ||
Line 34: | Line 26: | ||
readonly attribute WorkerGlobalScope self; | readonly attribute WorkerGlobalScope self; | ||
readonly attribute WorkerLocation location; | readonly attribute WorkerLocation location; | ||
readonly attribute boolean closing; | readonly attribute boolean closing; | ||
void close(); | void close(); | ||
Line 65: | 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 116: | Line 109: | ||
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. | 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. | |||
=== Proposal 2 === | === Proposal 2 === | ||
Line 125: | Line 147: | ||
[NoInterfaceObject] interface WorkerFactory { | [NoInterfaceObject] interface WorkerFactory { | ||
... | ... | ||
MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL); | |||
}; | |||
[NoInterfaceObject] interface WorkerGlobalScope { | |||
... | |||
attribute EventListener onconnect; | |||
}; | }; | ||
Line 132: | Line 159: | ||
</blockquote> | </blockquote> | ||
<code> | <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. | ||
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). | |||
The | 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. |