[whatwg] Define MessagePort.isConnected or MessagePort.ondisconnect

On Mon, 15 Mar 2010, ATSUSHI TAKAYAMA wrote:
> 
> 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.

We used to have that (onclose would fire on the port), but we removed it 
in the first version because it exposes garbage collection behaviour, 
which we try pretty hard not to expose, so as to prevent sites from 
accidentally depending on a particular GC strategy and thus forcing all 
browsers to implement (or fake) the same GC strategy.

If you don't need to ever broadcast something to all the ports, you can 
avoid keeping track of the ports altogether, and then you won't have a 
problem. If you do need to broadcast, it's hard not to slowly leak at the 
moment. We might add an object that can handle broadcast without leaking 
at some future point (basically an opaque array that you push ports into 
and which you can ask to iterate over the ports calling a function), but 
it doesn't exist currently.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Monday, 15 March 2010 17:42:48 UTC