mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
fix(op_crates/web): define abort event handler on prototype (#8230)
This commit is contained in:
parent
23557331c9
commit
bfa00bef22
2 changed files with 31 additions and 19 deletions
|
@ -36,24 +36,6 @@
|
|||
throw new TypeError("Illegal constructor.");
|
||||
}
|
||||
super();
|
||||
// HTML specification section 8.1.5.1
|
||||
let eventHandler = null;
|
||||
Object.defineProperty(this, "onabort", {
|
||||
get() {
|
||||
return eventHandler;
|
||||
},
|
||||
set(value) {
|
||||
if (eventHandler) {
|
||||
this.removeEventListener("abort", eventHandler);
|
||||
}
|
||||
eventHandler = value;
|
||||
if (typeof eventHandler === "function") {
|
||||
this.addEventListener("abort", value);
|
||||
}
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
});
|
||||
}
|
||||
|
||||
get aborted() {
|
||||
|
@ -64,7 +46,7 @@
|
|||
return "AbortSignal";
|
||||
}
|
||||
}
|
||||
|
||||
defineEventHandler(AbortSignal.prototype, "abort");
|
||||
class AbortController {
|
||||
#signal = new AbortSignal(illegalConstructorKey);
|
||||
|
||||
|
@ -81,6 +63,29 @@
|
|||
}
|
||||
}
|
||||
|
||||
const handlerSymbol = Symbol("eventHandlers");
|
||||
function defineEventHandler(emitter, name) {
|
||||
// HTML specification section 8.1.5.1
|
||||
Object.defineProperty(emitter, `on${name}`, {
|
||||
get() {
|
||||
return this[handlerSymbol]?.get(name);
|
||||
},
|
||||
set(value) {
|
||||
const oldListener = this[handlerSymbol]?.get(name);
|
||||
if (oldListener) {
|
||||
this.removeEventListener(name, oldListener);
|
||||
}
|
||||
if (!this[handlerSymbol]) {
|
||||
this[handlerSymbol] = new Map();
|
||||
}
|
||||
this.addEventListener(name, value);
|
||||
this[handlerSymbol].set(value);
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
});
|
||||
}
|
||||
|
||||
window.AbortSignal = AbortSignal;
|
||||
window.AbortController = AbortController;
|
||||
window.__bootstrap = window.__bootstrap || {};
|
||||
|
|
|
@ -95,6 +95,12 @@ function abortSignalEventOrder() {
|
|||
assertEquals(arr[1], 2);
|
||||
assertEquals(arr[2], 3);
|
||||
}
|
||||
function abortSignalHandlerLocation() {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
const abortHandler = Object.getOwnPropertyDescriptor(signal, "onabort");
|
||||
assertEquals(abortHandler, undefined);
|
||||
}
|
||||
function main() {
|
||||
basicAbortController();
|
||||
signalCallsOnabort();
|
||||
|
@ -103,6 +109,7 @@ function main() {
|
|||
controllerHasProperToString();
|
||||
abortSignalIllegalConstructor();
|
||||
abortSignalEventOrder();
|
||||
abortSignalHandlerLocation();
|
||||
}
|
||||
|
||||
main();
|
||||
|
|
Loading…
Reference in a new issue