From 882ac9eb79593ef1ecf6c687e97b26865c82878c Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 15 Jan 2024 12:50:47 +0100 Subject: [PATCH] Remove unload event. fixes #165 --- src/y-websocket.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/y-websocket.js b/src/y-websocket.js index ea2d307f..9c72a881 100644 --- a/src/y-websocket.js +++ b/src/y-websocket.js @@ -350,17 +350,15 @@ export class WebsocketProvider extends Observable { ) broadcastMessage(this, encoding.toUint8Array(encoder)) } - this._unloadHandler = () => { + this._exitHandler = () => { awarenessProtocol.removeAwarenessStates( this.awareness, [doc.clientID], - 'window unload' + 'app closed' ) } - if (typeof window !== 'undefined') { - window.addEventListener('unload', this._unloadHandler) - } else if (typeof process !== 'undefined') { - process.on('exit', this._unloadHandler) + if (typeof process !== 'undefined') { + process.on('exit', this._exitHandler) } awareness.on('update', this._awarenessUpdateHandler) this._checkInterval = /** @type {any} */ (setInterval(() => { @@ -400,10 +398,8 @@ export class WebsocketProvider extends Observable { } clearInterval(this._checkInterval) this.disconnect() - if (typeof window !== 'undefined') { - window.removeEventListener('unload', this._unloadHandler) - } else if (typeof process !== 'undefined') { - process.off('exit', this._unloadHandler) + if (typeof process !== 'undefined') { + process.off('exit', this._exitHandler) } this.awareness.off('update', this._awarenessUpdateHandler) this.doc.off('update', this._updateHandler)