[whatwg] workers

On Wed, Sep 24, 2008 at 8:27 PM, Aaron Boodman <aa at google.com> wrote:
> In the current design, there are three separate mechanisms to connect
> to and communicate with a worker:
>
> a) DedicatedWorker::sendMessage() + DedicatedWorkerGlobalScope::onmessage
>
> b) DedicatedWorker::startConversation() + DedicatedWorkerGlobalScope::onmessage
>
> c) new SharedWorker() + SharedWorkerGlobalScope::onconnect
>
> I would like to combine all of these into one common mechanism:

I spoke with Jonas on IRC today:
http://krijnhoetmer.nl/irc-logs/whatwg/20080924#l-379

Though I failed to convince him that we should simplify all these down
to just one mechanism, but he did support combining b) and c) with
something like "(my proposal - global onmessage) +
DedicatedWorker::sendMessage + DedicatedWorkerGlobalScope::onmessage".

That would look like this:

OUTSIDE

interface MessagePort {
 EventListener onmessage;
 EventListener onclose;
 void sendMessage(String message);
}

interface Worker {
 EventListener onload;  // informative only, you can sendMessage
before this happens
 EventListener onerror;  // only load errors, not script errors at runtime
 Port connect();
};

interface DedicatedWorker : Worker {
 void close();
 void sendMessage(String message);
};

INSIDE

interface WorkerContext {
 EventListener onclose;
 EventListener onconnect;  // fired each time connect() is called on a
corresponding Worker object

 readonly String name;
 void close();

 // + all the utils stuffs
};

interface DedicatedWorkerContext : WorkerContext {
  EventListener onmessage; // fires when someone calls
DedicatedWorker::sendMessage()
};

EXAMPLE SIMPLE USAGE

// outer
var worker = new Worker("foo.js");
worker.onmessage = function(e) {
  alert(e.message);
}
worker.sendMessage("hi!");

// inner
self.onmessage = function(e) {
  self.sendMessage("bye");
}

EXAMPLE LESS SIMPLE USAGE

// outer
var worker = new Worker("foo.js");
var port = worker.connect();
port.onmessage = function(e) {
  alert(e.message);
}
port.sendMessage("hi!");

// inner
self.onconnect = function(e) {
  e.port.onmessage = function() {
    e.port.sendMessage("bye");
  }
}

- a

Received on Wednesday, 24 September 2008 11:38:53 UTC