diff options
| author | Andreu Botella <abb@randomunok.com> | 2021-10-08 09:53:31 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-08 09:53:31 +0200 |
| commit | 74e5b68682d4d2503e4af5bac3b98067bc58f275 (patch) | |
| tree | e6885e8b9c7a32c481bb5c4fe334ac40fcb89563 /ext/broadcast_channel | |
| parent | 7e38ae17ea5dba600d0bf7bb6f6fafe7cd25befd (diff) | |
refactor: deduplicate `defineEventHandler` util (#12367)
Diffstat (limited to 'ext/broadcast_channel')
| -rw-r--r-- | ext/broadcast_channel/01_broadcast_channel.js | 49 |
1 files changed, 1 insertions, 48 deletions
diff --git a/ext/broadcast_channel/01_broadcast_channel.js b/ext/broadcast_channel/01_broadcast_channel.js index 42112c014..d1970b1eb 100644 --- a/ext/broadcast_channel/01_broadcast_channel.js +++ b/ext/broadcast_channel/01_broadcast_channel.js @@ -7,7 +7,7 @@ ((window) => { const core = window.Deno.core; const webidl = window.__bootstrap.webidl; - const { setTarget } = window.__bootstrap.event; + const { defineEventHandler, setTarget } = window.__bootstrap.event; const { DOMException } = window.__bootstrap.domException; const { ArrayPrototypeIndexOf, @@ -15,55 +15,8 @@ ArrayPrototypePush, Symbol, Uint8Array, - ObjectDefineProperty, - Map, - MapPrototypeSet, - MapPrototypeGet, - FunctionPrototypeCall, } = window.__bootstrap.primordials; - const handlerSymbol = Symbol("eventHandlers"); - function makeWrappedHandler(handler) { - function wrappedHandler(...args) { - if (typeof wrappedHandler.handler !== "function") { - return; - } - return FunctionPrototypeCall(wrappedHandler.handler, this, ...args); - } - wrappedHandler.handler = handler; - return wrappedHandler; - } - // TODO(lucacasonato) reuse when we can reuse code between web crates - function defineEventHandler(emitter, name) { - // HTML specification section 8.1.5.1 - ObjectDefineProperty(emitter, `on${name}`, { - get() { - // TODO(bnoordhuis) The "BroadcastChannel should have an onmessage - // event" WPT test expects that .onmessage !== undefined. Returning - // null makes it pass but is perhaps not exactly in the spirit. - if (!this[handlerSymbol]) { - return null; - } - return MapPrototypeGet(this[handlerSymbol], name)?.handler ?? null; - }, - set(value) { - if (!this[handlerSymbol]) { - this[handlerSymbol] = new Map(); - } - let handlerWrapper = MapPrototypeGet(this[handlerSymbol], name); - if (handlerWrapper) { - handlerWrapper.handler = value; - } else { - handlerWrapper = makeWrappedHandler(value); - this.addEventListener(name, handlerWrapper); - } - MapPrototypeSet(this[handlerSymbol], name, handlerWrapper); - }, - configurable: true, - enumerable: true, - }); - } - const _name = Symbol("[[name]]"); const _closed = Symbol("[[closed]]"); |
