Remove unload event. fixes #165

This commit is contained in:
Kevin Jahns 2024-01-15 12:50:47 +01:00
parent 1f6716b32d
commit 882ac9eb79

View File

@ -350,17 +350,15 @@ export class WebsocketProvider extends Observable {
) )
broadcastMessage(this, encoding.toUint8Array(encoder)) broadcastMessage(this, encoding.toUint8Array(encoder))
} }
this._unloadHandler = () => { this._exitHandler = () => {
awarenessProtocol.removeAwarenessStates( awarenessProtocol.removeAwarenessStates(
this.awareness, this.awareness,
[doc.clientID], [doc.clientID],
'window unload' 'app closed'
) )
} }
if (typeof window !== 'undefined') { if (typeof process !== 'undefined') {
window.addEventListener('unload', this._unloadHandler) process.on('exit', this._exitHandler)
} else if (typeof process !== 'undefined') {
process.on('exit', this._unloadHandler)
} }
awareness.on('update', this._awarenessUpdateHandler) awareness.on('update', this._awarenessUpdateHandler)
this._checkInterval = /** @type {any} */ (setInterval(() => { this._checkInterval = /** @type {any} */ (setInterval(() => {
@ -400,10 +398,8 @@ export class WebsocketProvider extends Observable {
} }
clearInterval(this._checkInterval) clearInterval(this._checkInterval)
this.disconnect() this.disconnect()
if (typeof window !== 'undefined') { if (typeof process !== 'undefined') {
window.removeEventListener('unload', this._unloadHandler) process.off('exit', this._exitHandler)
} else if (typeof process !== 'undefined') {
process.off('exit', this._unloadHandler)
} }
this.awareness.off('update', this._awarenessUpdateHandler) this.awareness.off('update', this._awarenessUpdateHandler)
this.doc.off('update', this._updateHandler) this.doc.off('update', this._updateHandler)