[whatwg] Define MessagePort.isConnected or MessagePort.ondisconnect

ATSUSHI TAKAYAMA taka.atsushi at googlemail.com
Mon Mar 15 17:13:05 PDT 2010


Hi all,

Consider a case where I have a SharedWorker script like below, and I
open two tabs that use this SharedWorker. Now myPorts.length is 2. If
I reload one of the two tabs, then myPorts.length is 3, isn't it? But
one of the three ports is already disconnected from the counterpart,
so postMessage'ing to the port is meaningless and I want to discard
reference to that port.

=== <JS> ===
var myPorts = [];

onconnect = function(e) {
  var port = e.ports[0];
  myPorts.push(port);

  port.onmessage = function(e) {
    myPorts.forEach(function(p) {
      if (p !== port)
        p.postMessage = e.data;
    });
  }
}
=== </JS> ===

It seems like the only way to know if a MessagePort is connected is to
actually send a message and wait for a reply. So
MessagePort.isConnected or MessagePort.ondisconnect would be nice to
have.

A. TAKAYAMA


More information about the whatwg mailing list