mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -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");
|
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) {
|
function defineEventHandler(emitter, name) {
|
||||||
// HTML specification section 8.1.5.1
|
// HTML specification section 8.1.5.1
|
||||||
Object.defineProperty(emitter, `on${name}`, {
|
Object.defineProperty(emitter, `on${name}`, {
|
||||||
get() {
|
get() {
|
||||||
return this[handlerSymbol]?.get(name);
|
return this[handlerSymbol]?.get(name)?.handler;
|
||||||
},
|
},
|
||||||
set(value) {
|
set(value) {
|
||||||
const oldListener = this[handlerSymbol]?.get(name);
|
|
||||||
if (oldListener) {
|
|
||||||
this.removeEventListener(name, oldListener);
|
|
||||||
}
|
|
||||||
if (!this[handlerSymbol]) {
|
if (!this[handlerSymbol]) {
|
||||||
this[handlerSymbol] = new Map();
|
this[handlerSymbol] = new Map();
|
||||||
}
|
}
|
||||||
this.addEventListener(name, value);
|
let handlerWrapper = this[handlerSymbol]?.get(name);
|
||||||
this[handlerSymbol].set(value);
|
if (handlerWrapper) {
|
||||||
|
handlerWrapper.handler = value;
|
||||||
|
} else {
|
||||||
|
handlerWrapper = makeWrappedHandler(value);
|
||||||
|
this.addEventListener(name, handlerWrapper);
|
||||||
|
}
|
||||||
|
this[handlerSymbol].set(name, handlerWrapper);
|
||||||
},
|
},
|
||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
|
|
|
@ -95,6 +95,23 @@ function abortSignalEventOrder() {
|
||||||
assertEquals(arr[1], 2);
|
assertEquals(arr[1], 2);
|
||||||
assertEquals(arr[2], 3);
|
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() {
|
function abortSignalHandlerLocation() {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const { signal } = controller;
|
const { signal } = controller;
|
||||||
|
@ -109,6 +126,7 @@ function main() {
|
||||||
controllerHasProperToString();
|
controllerHasProperToString();
|
||||||
abortSignalIllegalConstructor();
|
abortSignalIllegalConstructor();
|
||||||
abortSignalEventOrder();
|
abortSignalEventOrder();
|
||||||
|
abortSignalEventOrderComplex();
|
||||||
abortSignalHandlerLocation();
|
abortSignalHandlerLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue