Merge pull request #34 from Liquidibrium/master

Removed dependency on window
This commit is contained in:
Kevin Jahns 2022-04-24 14:55:50 +02:00 committed by GitHub
commit 3037783947
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 () {
@ -422,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)
}
}
}