Skip to content

Commit

Permalink
Remove unload event. fixes #165
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jan 15, 2024
1 parent 1f6716b commit 882ac9e
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 882ac9e

Please sign in to comment.