Skip to content

Commit

Permalink
feat(op_crates/web) EventTarget signal support (#8616)
Browse files Browse the repository at this point in the history
Fixes: #8606
  • Loading branch information
benjamingr authored Dec 4, 2020
1 parent ae21a95 commit 71ef5a9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
16 changes: 14 additions & 2 deletions op_crates/web/01_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
}

Expand Down
20 changes: 20 additions & 0 deletions op_crates/web/event_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -116,6 +135,7 @@ function main() {
eventInitializedWithNonStringType();
eventIsTrusted();
eventIsTrustedGetterName();
eventAbortSignal();
}

main();
2 changes: 1 addition & 1 deletion std/wasi/testdata

0 comments on commit 71ef5a9

Please sign in to comment.