Fix beforeunload memory leak

This commit is contained in:
Nemanja Tosic 2021-05-30 13:42:01 +02:00
parent 0007200b1d
commit 7afa2e6643

View File

@ -277,9 +277,10 @@ export class WebsocketProvider extends Observable {
broadcastMessage(this, encoding.toUint8Array(encoder)) broadcastMessage(this, encoding.toUint8Array(encoder))
} }
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', () => { this._beforeUnloadHandler = () => {
awarenessProtocol.removeAwarenessStates(this.awareness, [doc.clientID], 'window unload') awarenessProtocol.removeAwarenessStates(this.awareness, [doc.clientID], 'window unload')
}) };
window.addEventListener('beforeunload', this._beforeUnloadHandler)
} }
awareness.on('update', this._awarenessUpdateHandler) awareness.on('update', this._awarenessUpdateHandler)
this._checkInterval = /** @type {any} */ (setInterval(() => { this._checkInterval = /** @type {any} */ (setInterval(() => {
@ -315,6 +316,9 @@ export class WebsocketProvider extends Observable {
} }
clearInterval(this._checkInterval) clearInterval(this._checkInterval)
this.disconnect() this.disconnect()
if (this._beforeUnloadHandler) {
window.removeEventListener('beforeunload', this._beforeUnloadHandler)
}
this.awareness.off('update', this._awarenessUpdateHandler) this.awareness.off('update', this._awarenessUpdateHandler)
this.doc.off('update', this._updateHandler) this.doc.off('update', this._updateHandler)
super.destroy() super.destroy()