From c6a88a130b944e3b225bfa1c702baed3d5658496 Mon Sep 17 00:00:00 2001 From: Liquidibrium Date: Sun, 24 Apr 2022 10:39:28 +0400 Subject: [PATCH 1/2] Seperated _beforeUnloadHandler for NodeJS --- src/y-webrtc.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/y-webrtc.js b/src/y-webrtc.js index 5768779..37329f8 100644 --- a/src/y-webrtc.js +++ b/src/y-webrtc.js @@ -359,12 +359,18 @@ export class Room { broadcastRoomMessage(this, encoding.toUint8Array(encoderAwareness)) } - window.addEventListener('beforeunload', () => { + this._beforeUnloadHandler = () => { awarenessProtocol.removeAwarenessStates(this.awareness, [doc.clientID], 'window unload') rooms.forEach(room => { room.disconnect() }) - }) + } + + if (typeof window !== 'undefined') { + window.addEventListener('beforeunload', this._beforeUnloadHandler) + } else if (typeof process !== 'undefined') { + process.on('exit', this._beforeUnloadHandler) + } } connect () { From 017b9e8b8f25ef7857211bbe65920e509f6e8b23 Mon Sep 17 00:00:00 2001 From: Liquidibrium Date: Sun, 24 Apr 2022 10:43:16 +0400 Subject: [PATCH 2/2] Remove _beforeUnloadHandler after destroy --- src/y-webrtc.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/y-webrtc.js b/src/y-webrtc.js index 37329f8..ee09502 100644 --- a/src/y-webrtc.js +++ b/src/y-webrtc.js @@ -428,6 +428,11 @@ export class Room { destroy () { this.disconnect() + if (typeof window !== 'undefined') { + window.removeEventListener('beforeunload', this._beforeUnloadHandler) + } else if (typeof process !== 'undefined') { + process.off('exit', this._beforeUnloadHandler) + } } }