diff options
author | Benjamin Gruenbaum <benjamingr@gmail.com> | 2020-12-04 19:47:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 12:47:08 -0500 |
commit | 71ef5a9cd33390f7f41b80cdb57862b7f7507f76 (patch) | |
tree | e67a19826c27c41ea50919030b9b54d49a145cf1 | |
parent | ae21a9569b87411f863fa5194c873be69d8bee93 (diff) |
feat(op_crates/web) EventTarget signal support (#8616)
Fixes: https://github.com/denoland/deno/issues/8606
-rw-r--r-- | op_crates/web/01_event.js | 16 | ||||
-rw-r--r-- | op_crates/web/event_test.js | 20 | ||||
m--------- | std/wasi/testdata | 0 |
3 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(); diff --git a/std/wasi/testdata b/std/wasi/testdata -Subproject 8f49014513d9508f0495977be44ec60c6f4c8e0 +Subproject 4c7517f6cc5aa3bd7cf405be7dfb8ec1cac6d2d |