Merge pull request #56 from hanspagel/master
Add exit handler for Node.js environments, fix #51 #55
This commit is contained in:
commit
4006b777c8
@ -276,11 +276,13 @@ export class WebsocketProvider extends Observable {
|
|||||||
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients))
|
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients))
|
||||||
broadcastMessage(this, encoding.toUint8Array(encoder))
|
broadcastMessage(this, encoding.toUint8Array(encoder))
|
||||||
}
|
}
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
this._beforeUnloadHandler = () => {
|
this._beforeUnloadHandler = () => {
|
||||||
awarenessProtocol.removeAwarenessStates(this.awareness, [doc.clientID], 'window unload')
|
awarenessProtocol.removeAwarenessStates(this.awareness, [doc.clientID], 'window unload')
|
||||||
};
|
}
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
window.addEventListener('beforeunload', this._beforeUnloadHandler)
|
window.addEventListener('beforeunload', this._beforeUnloadHandler)
|
||||||
|
} else if (typeof process !== 'undefined') {
|
||||||
|
process.on('exit', () => this._beforeUnloadHandler)
|
||||||
}
|
}
|
||||||
awareness.on('update', this._awarenessUpdateHandler)
|
awareness.on('update', this._awarenessUpdateHandler)
|
||||||
this._checkInterval = /** @type {any} */ (setInterval(() => {
|
this._checkInterval = /** @type {any} */ (setInterval(() => {
|
||||||
@ -316,8 +318,10 @@ export class WebsocketProvider extends Observable {
|
|||||||
}
|
}
|
||||||
clearInterval(this._checkInterval)
|
clearInterval(this._checkInterval)
|
||||||
this.disconnect()
|
this.disconnect()
|
||||||
if (this._beforeUnloadHandler) {
|
if (typeof window !== 'undefined') {
|
||||||
window.removeEventListener('beforeunload', this._beforeUnloadHandler)
|
window.removeEventListener('beforeunload', this._beforeUnloadHandler)
|
||||||
|
} else if (typeof process !== 'undefined') {
|
||||||
|
process.off('exit', () => 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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user