Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessagePort API:add a close event #9933

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -102361,6 +102361,12 @@ new PaymentRequest(…); // Allowed to use
<li><p>Set <var>document</var>'s <i data-x="concept-document-salvageable">salvageable</i> state
to false.</p></li>

<li><p>Let <var>ports </var>be the list of <code>MessagePort</code>s whose <span>relevant global
object</span>'s <span data-x="concept-document-window">associated
<code>Document</code></span> is <var>document</var>.</p></li>
domenic marked this conversation as resolved.
Show resolved Hide resolved

<li><p>For each <var>port</var> in <var>ports</var>, <span>disentangle</span> <var>port</var>.</p></li>

<li><p>Run any <span>unloading document cleanup steps</span> for <var>document</var> that
are defined by this specification and <span>other applicable specifications</span>.</p></li>

Expand Down Expand Up @@ -114575,6 +114581,7 @@ interface <dfn interface>MessagePort</dfn> : <span>EventTarget</span> {
// event handlers
attribute <span>EventHandler</span> <span data-x="handler-MessagePort-onmessage">onmessage</span>;
attribute <span>EventHandler</span> <span data-x="handler-MessagePort-onmessageerror">onmessageerror</span>;
attribute <span>EventHandler</span> <span data-x="handler-MessagePort-onclose">onclose</span>;
};

dictionary <dfn dictionary>StructuredSerializeOptions</dfn> {
Expand Down Expand Up @@ -114669,6 +114676,25 @@ dictionary <dfn dictionary>StructuredSerializeOptions</dfn> {
</li>
</ol>

<p>The <dfn>disentangle</dfn> steps, given a <code>MessagePort</code> <var>initiatorPort</var> which initiates disentangling,
are as follows:</p>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<ol>
<li><p>Let <var>otherPort</var> be the <code>MessagePort</code> which <var>initiatorPort</var> was entangled with.</p></li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<li><p>Assert: <var>otherPort</var> exists.</p></li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<li><p>Disentangle <var>initiatorPort</var> and <var>otherPort</var>, so that they are no longer entangled or associated with each other.</p></li>
<li><p><span data-x="concept-event-dispatch">Dispatch</span> close event at <var>otherPort</var>.</p></li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<div class="note">
<p>The close event <!--non-normative-->must be fired even if the port is not explicitly closed. The cases where this event is dispatched are:</p>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<ul>
<li><code data-x="dom-MessagePort-close">close()</code> method was called.</li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<li>Document was <span data-x="destroy a document">destroyed.</span></li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
<li>Port was garbage collected.</li>
domenic marked this conversation as resolved.
Show resolved Hide resolved
domenic marked this conversation as resolved.
Show resolved Hide resolved
</ul>
<p>We only dispatch the event on <var>otherPort</var> because <var>initiatorPort</var> explicitly triggered the close, its document no longer exists, or
domenic marked this conversation as resolved.
Show resolved Hide resolved
it was already garbage collected, as described above.</p>
</div>
domenic marked this conversation as resolved.
Show resolved Hide resolved
</ol>

<hr>

<p id="transferMessagePort"><code>MessagePort</code> objects are <span>transferable
Expand Down Expand Up @@ -114829,7 +114855,7 @@ dictionary <dfn dictionary>StructuredSerializeOptions</dfn> {
<ol>
<li><p>Set <span>this</span>'s <span>[[Detached]]</span> internal slot value to true.</p></li>

<li><p>If <span>this</span> is entangled, disentangle it.</p></li>
<li><p>If <span>this</span> is entangled, <span>disentangle</span> it.</p></li>
</ol>

<hr>
Expand All @@ -114845,6 +114871,7 @@ dictionary <dfn dictionary>StructuredSerializeOptions</dfn> {
<tbody>
<tr><td><dfn attribute for="MessagePort"><code data-x="handler-MessagePort-onmessage">onmessage</code></dfn> <td> <code data-x="event-message">message</code>
<tr><td><dfn attribute for="MessagePort"><code data-x="handler-MessagePort-onmessageerror">onmessageerror</code></dfn> <td> <code data-x="event-messageerror">messageerror</code>
<tr><td><dfn attribute for="MessagePort"><code data-x="handler-MessagePort-onclose">onclose</code></dfn> <td> <code data-x="event-close">close</code>
</table>

<p>The first time a <code>MessagePort</code> object's <code
Expand All @@ -114855,19 +114882,6 @@ dictionary <dfn dictionary>StructuredSerializeOptions</dfn> {
</div>


<h4>Broadcasting to many ports</h4>

<!-- NON-NORMATIVE SECTION -->

<p>Broadcasting to many ports is in principle relatively simple: keep an array of
<code>MessagePort</code> objects to send messages to, and iterate through the array to send a
message. However, this has one rather unfortunate effect: it prevents the ports from being garbage
collected, even if the other side has gone away. To avoid this problem, implement a simple
protocol whereby the other side acknowledges it still exists. If it doesn't do so after a certain
amount of time, assume it's gone, close the <code>MessagePort</code> object, and let it be garbage
collected.</p>


<h4 id="ports-and-garbage-collection">Ports and garbage collection</h4>

<div w-nodev>
domenic marked this conversation as resolved.
Show resolved Hide resolved
Expand Down