mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
fix(op_crates/web): handler order when reassign (#8264)
This commit is contained in:
parent
c650c5c720
commit
996a87b168
2 changed files with 38 additions and 7 deletions
|
@ -64,22 +64,35 @@
|
|||
}
|
||||
|
||||
const handlerSymbol = Symbol("eventHandlers");
|
||||
|
||||
function makeWrappedHandler(handler) {
|
||||
function wrappedHandler(...args) {
|
||||
if (typeof wrappedHandler.handler !== "function") {
|
||||
return;
|
||||
}
|
||||
return wrappedHandler.handler.call(this, ...args);
|
||||
}
|
||||
wrappedHandler.handler = handler;
|
||||
return wrappedHandler;
|
||||
}
|
||||
function defineEventHandler(emitter, name) {
|
||||
// HTML specification section 8.1.5.1
|
||||
Object.defineProperty(emitter, `on${name}`, {
|
||||
get() {
|
||||
return this[handlerSymbol]?.get(name);
|
||||
return this[handlerSymbol]?.get(name)?.handler;
|
||||
},
|
||||
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);
|
||||
let handlerWrapper = this[handlerSymbol]?.get(name);
|
||||
if (handlerWrapper) {
|
||||
handlerWrapper.handler = value;
|
||||
} else {
|
||||
handlerWrapper = makeWrappedHandler(value);
|
||||
this.addEventListener(name, handlerWrapper);
|
||||
}
|
||||
this[handlerSymbol].set(name, handlerWrapper);
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
|
|
|
@ -95,6 +95,23 @@ function abortSignalEventOrder() {
|
|||
assertEquals(arr[1], 2);
|
||||
assertEquals(arr[2], 3);
|
||||
}
|
||||
|
||||
function abortSignalEventOrderComplex() {
|
||||
const arr = [];
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
signal.addEventListener("abort", () => arr.push(1));
|
||||
signal.onabort = () => {
|
||||
throw new Error();
|
||||
};
|
||||
signal.addEventListener("abort", () => arr.push(3));
|
||||
signal.onabort = () => arr.push(2);
|
||||
controller.abort();
|
||||
assertEquals(arr[0], 1);
|
||||
assertEquals(arr[1], 2);
|
||||
assertEquals(arr[2], 3);
|
||||
}
|
||||
|
||||
function abortSignalHandlerLocation() {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
|
@ -109,6 +126,7 @@ function main() {
|
|||
controllerHasProperToString();
|
||||
abortSignalIllegalConstructor();
|
||||
abortSignalEventOrder();
|
||||
abortSignalEventOrderComplex();
|
||||
abortSignalHandlerLocation();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue