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/websocket/01_websocket.js | |
parent | 7e38ae17ea5dba600d0bf7bb6f6fafe7cd25befd (diff) |
refactor: deduplicate `defineEventHandler` util (#12367)
Diffstat (limited to 'ext/websocket/01_websocket.js')
-rw-r--r-- | ext/websocket/01_websocket.js | 45 |
1 files changed, 1 insertions, 44 deletions
diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 54e05c408..200bb8659 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -9,6 +9,7 @@ const webidl = window.__bootstrap.webidl; const { HTTP_TOKEN_CODE_POINT_RE } = window.__bootstrap.infra; const { DOMException } = window.__bootstrap.domException; + const { defineEventHandler } = window.__bootstrap.event; const { Blob } = globalThis.__bootstrap.file; const { ArrayBuffer, @@ -16,16 +17,11 @@ ArrayPrototypeJoin, DataView, ErrorPrototypeToString, - ObjectDefineProperty, - Map, - MapPrototypeGet, - MapPrototypeSet, Set, Symbol, String, StringPrototypeToLowerCase, StringPrototypeEndsWith, - FunctionPrototypeCall, RegExpPrototypeTest, ObjectDefineProperties, ArrayPrototypeMap, @@ -65,45 +61,6 @@ const CLOSING = 2; const CLOSED = 3; - 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() { - if (!this[handlerSymbol]) { - return null; - } - return MapPrototypeGet(this[handlerSymbol], name)?.handler; - }, - 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 _readyState = Symbol("[[readyState]]"); const _url = Symbol("[[url]]"); const _rid = Symbol("[[rid]]"); |