[whatwg] Define MessagePort.isConnected or MessagePort.ondisconnect

Agreed, there's not a good way to determine that a port is
disentangled. Currently the main solution I know of is to have your document
post a message to your shared worker in their onunload handler.

I think some kind of MessagePort.onclose event or "entangled" attribute
could be useful - this was originally part of the spec, and the issue with
that was that it's hard to define onclose in such a way that doesn't make it
highly dependent on garbage collection.

As an example:

var channel = new MessageChannel();
channel.port1.onclose = channel.port2.onclose = function() {alert("port
closed");};
channel = null;

What should happen in this case? At what point (if ever) should the onclose
handler be invoked?

I'm just leery of any situation where the garbage collected state of an
unreferenced object is exposed to script, as it seems like this causes
interoperability issues. For example, if you ran the script above in Chrome,
the onclose handler would likely not be invoked until the parent Document
was closed. In Safari, it would get invoked when the JS heap is next garbage
collected. An application that relied on onclose() being called in a timely
manner would break on Chrome.

The only option that comes to mind that doesn't expose compatibility issues
would be to only issue onclose events if close() is explicitly called on the
entangled port, but if you're doing that you might as well just have the
code calling close() post a "I'm closing" message first.

-atw


On Mon, Mar 15, 2010 at 5:13 PM, ATSUSHI TAKAYAMA <
taka.atsushi at googlemail.com> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20100315/aad31546/attachment.htm>

Received on Monday, 15 March 2010 17:44:50 UTC