1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

fix(op_crates/web): handler order when reassign (#8264)

This commit is contained in:
Benjamin Gruenbaum 2020-11-09 18:49:19 +02:00 committed by GitHub
parent c650c5c720
commit 996a87b168
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

View file

@ -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,

View file

@ -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();
}