Skip to content

Commit 83037a1

Browse files
authored
Add signal support to addEventListener()
Tests: web-platform-tests/wpt#26472. Fixes #911.
1 parent 73e7f33 commit 83037a1

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

dom.bs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ function imgFetched(ev) {
278278
<a>Event listeners</a> can be removed
279279
by utilizing the
280280
{{EventTarget/removeEventListener()}}
281-
method, passing the same arguments.
281+
method passing the same arguments.
282+
283+
Alternatively, <a>event listeners</a> can be removed by passing an {{AbortSignal}} to
284+
{{EventTarget/addEventListener()}} and calling {{AbortController/abort()}} on the controller
285+
owning the signal.
282286

283287
<a>Events</a> are objects too and implement the
284288
{{Event}} interface (or a derived interface). In the example above
@@ -962,6 +966,7 @@ dictionary EventListenerOptions {
962966
dictionary AddEventListenerOptions : EventListenerOptions {
963967
boolean passive = false;
964968
boolean once = false;
969+
AbortSignal signal;
965970
};
966971
</pre>
967972

@@ -981,6 +986,7 @@ when something has occurred.
981986
<li><dfn for="event listener">capture</dfn> (a boolean, initially false)
982987
<li><dfn for="event listener">passive</dfn> (a boolean, initially false)
983988
<li><dfn for="event listener">once</dfn> (a boolean, initially false)
989+
<li><dfn for="event listener">signal</dfn> (null or an {{AbortSignal}} object)
984990
<li><dfn for="event listener">removed</dfn> (a boolean for bookkeeping purposes, initially false)
985991
</ul>
986992

@@ -1042,6 +1048,9 @@ are not to be used for anything else. [[!HTML]]
10421048
<a for="event listener">callback</a> will only be invoked once after which the event listener will
10431049
be removed.
10441050

1051+
<p>If an {{AbortSignal}} is passed for <var>options</var>'s {{AddEventListenerOptions/signal}},
1052+
then the event listener will be removed when signal is aborted.
1053+
10451054
<p>The <a>event listener</a> is appended to <var>target</var>'s
10461055
<a for=EventTarget>event listener list</a> and is not appended if it has the same
10471056
<a for="event listener">type</a>, <a for="event listener">callback</a>, and
@@ -1064,7 +1073,7 @@ steps:
10641073
<ol>
10651074
<li><p>If <var>options</var> is a boolean, then return <var>options</var>.
10661075

1067-
<li><p>Return <var>options</var>'s <code>{{EventListenerOptions/capture}}</code>.
1076+
<li><p>Return |options|["{{EventListenerOptions/capture}}"].
10681077
</ol>
10691078

10701079
<p>To <dfn export for=Event>flatten more</dfn><!-- sorry --> <var>options</var>, run these
@@ -1075,11 +1084,20 @@ steps:
10751084

10761085
<li><p>Let <var>once</var> and <var>passive</var> be false.
10771086

1078-
<li><p>If <var>options</var> is a dictionary, then set <var>passive</var> to <var>options</var>'s
1079-
<code>{{AddEventListenerOptions/passive}}</code> and <var>once</var> to <var>options</var>'s
1080-
<code>{{AddEventListenerOptions/once}}</code>.
1087+
<li><p>Let |signal| be null.
1088+
1089+
<li>
1090+
<p>If |options| is a dictionary, then:
1091+
1092+
<ol>
1093+
<li><p>Set |passive| to |options|["{{AddEventListenerOptions/passive}}"] and |once| to
1094+
|options|["{{AddEventListenerOptions/once}}"].
1095+
1096+
<li><p>If |options|["{{AddEventListenerOptions/signal}}"] [=map/exists=], then set |signal| to
1097+
|options|["{{AddEventListenerOptions/signal}}"].
1098+
</ol>
10811099

1082-
<li><p>Return <var>capture</var>, <var>passive</var>, and <var>once</var>.
1100+
<li><p>Return <var>capture</var>, <var>passive</var>, <var>once</var>, and <var>signal</var>.
10831101
</ol>
10841102

10851103
<p>The <dfn constructor for=EventTarget><code>EventTarget()</code></dfn> constructor, when invoked,
@@ -1105,6 +1123,9 @@ participate in a tree structure.</p>
11051123
<a>service worker events</a>, then <a>report a warning to the console</a> that this might not give
11061124
the expected results. [[!SERVICE-WORKERS]]
11071125

1126+
<li><p>If <a for="event listener">signal</a> is not null and its [=AbortSignal/aborted flag=] is
1127+
set, then return.
1128+
11081129
<li><p>If <var>listener</var>'s <a for="event listener">callback</a> is null, then return.
11091130

11101131
<li><p>If <var>eventTarget</var>'s <a>event listener list</a> does not <a for=list>contain</a> an
@@ -1113,6 +1134,14 @@ participate in a tree structure.</p>
11131134
<a for="event listener">callback</a>, and <a for="event listener">capture</a> is
11141135
<var>listener</var>'s <a for="event listener">capture</a>, then <a for=list>append</a>
11151136
<var>listener</var> to <var>eventTarget</var>'s <a>event listener list</a>.
1137+
1138+
<li>
1139+
<p>If <var>listener</var>'s <a for="event listener">signal</a> is not null, then
1140+
<a for=AbortSignal lt=add>add the following</a> abort steps to it:
1141+
1142+
<ol>
1143+
<li><a>Remove an event listener</a> with <var>eventTarget</var> and <var>listener</var>.
1144+
</ol>
11161145
</ol>
11171146

11181147
<p class=note>The <a>add an event listener</a> concept exists to ensure <a>event handlers</a> use
@@ -1129,8 +1158,8 @@ method, when invoked, must run these steps:
11291158
<li><p><a>Add an event listener</a> with <a>this</a> and an <a>event listener</a> whose
11301159
<a for="event listener">type</a> is <var>type</var>, <a for="event listener">callback</a> is
11311160
<var>callback</var>, <a for="event listener">capture</a> is <var>capture</var>,
1132-
<a for="event listener">passive</a> is <var>passive</var>, and <a for="event listener">once</a> is
1133-
<var>once</var>.
1161+
<a for="event listener">passive</a> is <var>passive</var>, <a for="event listener">once</a> is
1162+
<var>once</var>, and <a for="event listener">signal</a> is <var>signal</var>.
11341163
</ol>
11351164

11361165
<p>To <dfn export>remove an event listener</dfn>, given an {{EventTarget}} object
@@ -9959,6 +9988,7 @@ Anthony Ramine,
99599988
Arkadiusz Michalski,
99609989
Arnaud Le Hors,
99619990
Arun Ranganathan,
9991+
Benjamin Gruenbaum,
99629992
Björn Höhrmann,
99639993
Boris Zbarsky,
99649994
Brandon Payton,

0 commit comments

Comments
 (0)