mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
simplify close impl
This commit is contained in:
parent
9692d3facf
commit
d4010d8199
1 changed files with 8 additions and 26 deletions
|
@ -67,8 +67,14 @@ export class AdriftBareClient extends Client {
|
||||||
webSocketImpl: WebSocketImpl
|
webSocketImpl: WebSocketImpl
|
||||||
): WebSocket {
|
): WebSocket {
|
||||||
const ws = new webSocketImpl("ws:null", protocols);
|
const ws = new webSocketImpl("ws:null", protocols);
|
||||||
const closeEmitter = new EventTarget();
|
|
||||||
// this will error. that's okay
|
// 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(
|
let { send, close } = this.connection.wsconnect(
|
||||||
remote,
|
remote,
|
||||||
|
@ -78,7 +84,6 @@ export class AdriftBareClient extends Client {
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
onReadyState(WebSocket.CLOSED);
|
onReadyState(WebSocket.CLOSED);
|
||||||
closeEmitter.dispatchEvent(new Event("close"));
|
|
||||||
},
|
},
|
||||||
(data) => {
|
(data) => {
|
||||||
ws.dispatchEvent(
|
ws.dispatchEvent(
|
||||||
|
@ -100,32 +105,9 @@ export class AdriftBareClient extends Client {
|
||||||
"close",
|
"close",
|
||||||
() => (code?: number, reason?: string) => {
|
() => (code?: number, reason?: string) => {
|
||||||
close(code, reason);
|
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;
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue