summaryrefslogtreecommitdiff
path: root/op_crates
diff options
context:
space:
mode:
Diffstat (limited to 'op_crates')
-rw-r--r--op_crates/web/01_event.js16
-rw-r--r--op_crates/web/event_test.js20
2 files changed, 34 insertions, 2 deletions
diff --git a/op_crates/web/01_event.js b/op_crates/web/01_event.js
index ed76a4281..35b269ea4 100644
--- a/op_crates/web/01_event.js
+++ b/op_crates/web/01_event.js
@@ -693,7 +693,7 @@
for (let i = 0; i < handlers.length; i++) {
const listener = handlers[i];
- let capture, once, passive;
+ let capture, once, passive, signal;
if (typeof listener.options === "boolean") {
capture = listener.options;
once = false;
@@ -895,7 +895,19 @@
return;
}
}
-
+ if (options?.signal) {
+ const signal = options?.signal;
+ if (signal.aborted) {
+ // If signal is not null and its aborted flag is set, then return.
+ return;
+ } else {
+ // If listener’s signal is not null, then add the following abort
+ // abort steps to it: Remove an event listener.
+ signal.addEventListener("abort", () => {
+ this.removeEventListener(type, callback, options);
+ });
+ }
+ }
listeners[type].push({ callback, options });
}
diff --git a/op_crates/web/event_test.js b/op_crates/web/event_test.js
index 8107f3bca..00459c442 100644
--- a/op_crates/web/event_test.js
+++ b/op_crates/web/event_test.js
@@ -106,6 +106,25 @@ function eventIsTrustedGetterName() {
assert(e.message.includes("not a constructor"));
}
}
+function eventAbortSignal() {
+ let count = 0;
+ function handler() {
+ count++;
+ }
+ const et = new EventTarget();
+ const controller = new AbortController();
+ et.addEventListener("test", handler, { signal: controller.signal });
+ et.dispatchEvent(new Event("test"));
+ assert(count === 1);
+ et.dispatchEvent(new Event("test"));
+ assert(count === 2);
+ controller.abort();
+ et.dispatchEvent(new Event("test"));
+ assert(count === 2);
+ et.addEventListener("test", handler, { signal: controller.signal });
+ et.dispatchEvent(new Event("test"));
+ assert(count === 2);
+}
function main() {
eventInitializedWithType();
eventInitializedWithTypeAndDict();
@@ -116,6 +135,7 @@ function main() {
eventInitializedWithNonStringType();
eventIsTrusted();
eventIsTrustedGetterName();
+ eventAbortSignal();
}
main();