Skip to content

Commit

Permalink
Remove duplication, allow lexical context, fix default setter behavior
Browse files Browse the repository at this point in the history
Also only call `removeEventListener` in handleDisconnect() when
actually registered before.
  • Loading branch information
Philzen committed Dec 18, 2024
1 parent b88a644 commit 14a59e9
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.setupE2EE();
}

const self = this
this.options = new Proxy(this.options, {
set: function (target: InternalRoomOptions, key: keyof InternalRoomOptions, value) {
set: (target: InternalRoomOptions, key: keyof InternalRoomOptions, value) => {
console.log("Proxy is intercepting!", key, value)
if (key == 'disconnectOnPageLeave' && value !== target[key]) {
value === true
? window.addEventListener('beforeunload', self.onPageLeave)
: window.removeEventListener('beforeunload', self.onPageLeave)
value === true ? this.registerUnloadEvents() : this.unregisterUnloadEvents()
}


(target[key] as any) = value

return true;
}
})
Expand Down Expand Up @@ -774,13 +774,8 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
throw e;
}

// also hook unload event
if (isWeb() && this.options.disconnectOnPageLeave) {
// capturing both 'pagehide' and 'beforeunload' to capture broadest set of browser behaviors
window.addEventListener('pagehide', this.onPageLeave);
window.addEventListener('beforeunload', this.onPageLeave);
}
if (isWeb()) {
this.options.disconnectOnPageLeave && this.registerUnloadEvents()
document.addEventListener('freeze', this.onPageLeave);
navigator.mediaDevices?.addEventListener('devicechange', this.handleDeviceChange);
}
Expand Down Expand Up @@ -971,6 +966,23 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
}
}

/**
* Add listeners for 'pagehide' and 'beforeunload' events (capturing broadest set of browser behaviors)
* to handle disconnect and cleanup
*/
private registerUnloadEvents = () => {
window.addEventListener('pagehide', this.onPageLeave);
window.addEventListener('beforeunload', this.onPageLeave);
}

/**
* Remove listeners for 'pagehide' and 'beforeunload' which would handle disconnect and cleanup
*/
private unregisterUnloadEvents = () => {
window.removeEventListener('beforeunload', this.onPageLeave);
window.removeEventListener('pagehide', this.onPageLeave);
}

private onPageLeave = async () => {
this.log.info('Page leave detected, disconnecting', this.logContext);
await this.disconnect();
Expand Down Expand Up @@ -1398,8 +1410,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.audioContext = undefined;
}
if (isWeb()) {
window.removeEventListener('beforeunload', this.onPageLeave);
window.removeEventListener('pagehide', this.onPageLeave);
this.options.disconnectOnPageLeave && this.unregisterUnloadEvents()
window.removeEventListener('freeze', this.onPageLeave);
navigator.mediaDevices?.removeEventListener('devicechange', this.handleDeviceChange);
}
Expand Down

0 comments on commit 14a59e9

Please sign in to comment.