From d4010d8199cfd1c0e271d1bbe70e03d4cf776dbd Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Mon, 14 Aug 2023 09:31:03 -0700 Subject: [PATCH] simplify close impl --- client/src/AdriftClient.ts | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/client/src/AdriftClient.ts b/client/src/AdriftClient.ts index c3790c4..021ac7f 100644 --- a/client/src/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -67,8 +67,14 @@ export class AdriftBareClient extends Client { webSocketImpl: WebSocketImpl ): WebSocket { const ws = new webSocketImpl("ws:null", protocols); - const closeEmitter = new EventTarget(); // this will error. that's okay + let initalCloseHappened = false; + ws.addEventListener("close", (e) => { + if (!initalCloseHappened) { + e.stopImmediatePropagation(); + initalCloseHappened = true; + } + }); let { send, close } = this.connection.wsconnect( remote, @@ -78,7 +84,6 @@ export class AdriftBareClient extends Client { }, () => { onReadyState(WebSocket.CLOSED); - closeEmitter.dispatchEvent(new Event("close")); }, (data) => { ws.dispatchEvent( @@ -100,32 +105,9 @@ export class AdriftBareClient extends Client { "close", () => (code?: number, reason?: string) => { close(code, reason); + onReadyState(WebSocket.CLOSING); } ); - let onClose = ws.onclose; - (ws as any).__defineGetter__("onclose", () => onClose); - (ws as any).__defineSetter__("onclose", (newOnClose: any) => { - onClose = newOnClose; - }); - closeEmitter.addEventListener("close", () => { - if (onClose) onClose.call(ws, new Event("close")); - }); - const _addEventListener = ws.addEventListener.bind(ws); - (ws as any).addEventListener = (evt: string, cb: any, options: any) => { - if (evt == "close") { - closeEmitter.addEventListener("close", cb, options); - return; - } - _addEventListener(evt, cb, options); - }; - const _removeEventListener = ws.removeEventListener.bind(ws); - (ws as any).removeEventListener = (evt: string, cb: any, options: any) => { - if (evt == "close") { - closeEmitter.removeEventListener("close", cb, options); - return; - } - _removeEventListener(evt, cb, options); - }; return ws; }