Currently, there is no timely and reliable way to detect when a MessagePort becomes disentangled. This makes it difficult to release resources associated with ports. Some users have used the unload event to detect disconnect, but it is deprecated. Therefore, The approach available now is to hold a MessagePort in WeakRef and periodically check if this port has been garbage collected by using deref() or to make use of a page-hide event, which is quite inefficient.
So, I propose adding a close event. For example, if there are two entangled ports, portA and portB, and portA is closed, the close event is fired on portB.
WebKittens
@annevk
Title of the spec
MessagePort close event
URL to the spec
https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
URL to the spec's repository
No response
Issue Tracker URL
https://github.com/whatwg/html/issues/1766
Explainer URL
https://github.com/fergald/explainer-messageport-close
TAG Design Review URL
No response
Mozilla standards-positions issue URL
No response
WebKit Bugzilla URL
No response
Radar URL
No response
Description
Currently, there is no timely and reliable way to detect when a MessagePort becomes disentangled. This makes it difficult to release resources associated with ports. Some users have used the unload event to detect disconnect, but it is deprecated. Therefore, The approach available now is to hold a MessagePort in WeakRef and periodically check if this port has been garbage collected by using deref() or to make use of a page-hide event, which is quite inefficient.
So, I propose adding a close event. For example, if there are two entangled ports, portA and portB, and portA is closed, the close event is fired on portB.