Skip to content

Commit

Permalink
Merge pull request #34 from Liquidibrium/master
Browse files Browse the repository at this point in the history
Removed dependency on window
  • Loading branch information
dmonad authored Apr 24, 2022
2 parents c059b86 + 017b9e8 commit 3037783
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/y-webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 3037783

Please sign in to comment.