W3C home > Mailing lists > Public > whatwg@whatwg.org > March 2010

[whatwg] Define MessagePort.isConnected or MessagePort.ondisconnect

From: ATSUSHI TAKAYAMA <taka.atsushi@googlemail.com>
Date: Mon, 15 Mar 2010 20:13:05 -0400
Message-ID: <9cef076f1003151713k5436e613m6bc4adec414915e2@mail.gmail.com>
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
Received on Monday, 15 March 2010 17:13:05 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:21 UTC