From 83037a112f3f78f071e9c3ad7d979e85dc7e7283 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 3 Dec 2020 18:05:00 +0200 Subject: [PATCH] Add signal support to addEventListener() Tests: https://github.com/web-platform-tests/wpt/pull/26472. Fixes #911. --- dom.bs | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/dom.bs b/dom.bs index b64858227..634db8ec7 100644 --- a/dom.bs +++ b/dom.bs @@ -278,7 +278,11 @@ function imgFetched(ev) { Event listeners can be removed by utilizing the {{EventTarget/removeEventListener()}} -method, passing the same arguments. +method passing the same arguments. + +Alternatively, event listeners can be removed by passing an {{AbortSignal}} to +{{EventTarget/addEventListener()}} and calling {{AbortController/abort()}} on the controller +owning the signal. Events are objects too and implement the {{Event}} interface (or a derived interface). In the example above @@ -962,6 +966,7 @@ dictionary EventListenerOptions { dictionary AddEventListenerOptions : EventListenerOptions { boolean passive = false; boolean once = false; + AbortSignal signal; }; @@ -981,6 +986,7 @@ when something has occurred.
  • capture (a boolean, initially false)
  • passive (a boolean, initially false)
  • once (a boolean, initially false) +
  • signal (null or an {{AbortSignal}} object)
  • removed (a boolean for bookkeeping purposes, initially false) @@ -1042,6 +1048,9 @@ are not to be used for anything else. [[!HTML]] callback will only be invoked once after which the event listener will be removed. +

    If an {{AbortSignal}} is passed for options's {{AddEventListenerOptions/signal}}, + then the event listener will be removed when signal is aborted. +

    The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and @@ -1064,7 +1073,7 @@ steps:

    1. If options is a boolean, then return options. -

    2. Return options's {{EventListenerOptions/capture}}. +

    3. Return |options|["{{EventListenerOptions/capture}}"].

    To flatten more options, run these @@ -1075,11 +1084,20 @@ steps:

  • Let once and passive be false. -

  • If options is a dictionary, then set passive to options's - {{AddEventListenerOptions/passive}} and once to options's - {{AddEventListenerOptions/once}}. +

  • Let |signal| be null. + +

  • +

    If |options| is a dictionary, then: + +

      +
    1. Set |passive| to |options|["{{AddEventListenerOptions/passive}}"] and |once| to + |options|["{{AddEventListenerOptions/once}}"]. + +

    2. If |options|["{{AddEventListenerOptions/signal}}"] [=map/exists=], then set |signal| to + |options|["{{AddEventListenerOptions/signal}}"]. +

    -
  • Return capture, passive, and once. +

  • Return capture, passive, once, and signal.

    The EventTarget() constructor, when invoked, @@ -1105,6 +1123,9 @@ participate in a tree structure.

    service worker events, then report a warning to the console that this might not give the expected results. [[!SERVICE-WORKERS]] +
  • If signal is not null and its [=AbortSignal/aborted flag=] is + set, then return. +

  • If listener's callback is null, then return.

  • If eventTarget's event listener list does not contain an @@ -1113,6 +1134,14 @@ participate in a tree structure.

    callback, and capture is listener's capture, then append listener to eventTarget's event listener list. + +
  • +

    If listener's signal is not null, then + add the following abort steps to it: + +

      +
    1. Remove an event listener with eventTarget and listener. +

    The add an event listener concept exists to ensure event handlers use @@ -1129,8 +1158,8 @@ method, when invoked, must run these steps:

  • Add an event listener with this and an event listener whose type is type, callback is callback, capture is capture, - passive is passive, and once is - once. + passive is passive, once is + once, and signal is signal.

    To remove an event listener, given an {{EventTarget}} object @@ -9959,6 +9988,7 @@ Anthony Ramine, Arkadiusz Michalski, Arnaud Le Hors, Arun Ranganathan, +Benjamin Gruenbaum, Björn Höhrmann, Boris Zbarsky, Brandon Payton,