From bfa00bef220fd26bed26940cdcaf5edf4bf83e13 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 6 Nov 2020 03:40:36 +0200 Subject: [PATCH] fix(op_crates/web): define abort event handler on prototype (#8230) --- op_crates/web/02_abort_signal.js | 43 ++++++++++++++------------ op_crates/web/abort_controller_test.js | 7 +++++ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/op_crates/web/02_abort_signal.js b/op_crates/web/02_abort_signal.js index a963b1807f..53745c52ba 100644 --- a/op_crates/web/02_abort_signal.js +++ b/op_crates/web/02_abort_signal.js @@ -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 || {}; diff --git a/op_crates/web/abort_controller_test.js b/op_crates/web/abort_controller_test.js index f9ff3d1666..4d37419860 100644 --- a/op_crates/web/abort_controller_test.js +++ b/op_crates/web/abort_controller_test.js @@ -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();