1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

feat(op_crates/web) EventTarget signal support (#8616)

Fixes: https://github.com/denoland/deno/issues/8606
This commit is contained in:
Benjamin Gruenbaum 2020-12-04 19:47:08 +02:00 committed by GitHub
parent ae21a9569b
commit 71ef5a9cd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View file

@ -693,7 +693,7 @@
for (let i = 0; i < handlers.length; i++) { for (let i = 0; i < handlers.length; i++) {
const listener = handlers[i]; const listener = handlers[i];
let capture, once, passive; let capture, once, passive, signal;
if (typeof listener.options === "boolean") { if (typeof listener.options === "boolean") {
capture = listener.options; capture = listener.options;
once = false; once = false;
@ -895,7 +895,19 @@
return; 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 listeners 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 }); listeners[type].push({ callback, options });
} }

View file

@ -106,6 +106,25 @@ function eventIsTrustedGetterName() {
assert(e.message.includes("not a constructor")); 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() { function main() {
eventInitializedWithType(); eventInitializedWithType();
eventInitializedWithTypeAndDict(); eventInitializedWithTypeAndDict();
@ -116,6 +135,7 @@ function main() {
eventInitializedWithNonStringType(); eventInitializedWithNonStringType();
eventIsTrusted(); eventIsTrusted();
eventIsTrustedGetterName(); eventIsTrustedGetterName();
eventAbortSignal();
} }
main(); main();

@ -1 +1 @@
Subproject commit 8f49014513d9508f0495977be44ec60c6f4c8e06 Subproject commit 4c7517f6cc5aa3bd7cf405be7dfb8ec1cac6d2de