Skip to content

[feature] Allow changing arbitrary options during room lifetime (disconnectOnPageLeave implemented) #1358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
if (this.options.e2ee) {
this.setupE2EE();
}

/**
* Proxy for interception of changes to InternalRoomOptions after Room has been instantiated.
*/
this.options = new Proxy(this.options, {
set: (target: InternalRoomOptions, key: keyof InternalRoomOptions, value) => {
if (key == 'disconnectOnPageLeave' && value !== target[key]) {
if (value === true) {
this.registerUnloadEvents();
} else {
this.unregisterUnloadEvents();
}
}

(target[key] as any) = value;

return true;
},
});
}

/**
Expand Down Expand Up @@ -761,13 +780,10 @@ 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()) {
if (this.options.disconnectOnPageLeave) {
this.registerUnloadEvents();
}
document.addEventListener('freeze', this.onPageLeave);
navigator.mediaDevices?.addEventListener('devicechange', this.handleDeviceChange);
}
Expand Down Expand Up @@ -958,6 +974,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 @@ -1385,8 +1418,9 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.audioContext = undefined;
}
if (isWeb()) {
window.removeEventListener('beforeunload', this.onPageLeave);
window.removeEventListener('pagehide', this.onPageLeave);
if (this.options.disconnectOnPageLeave) {
this.unregisterUnloadEvents();
}
window.removeEventListener('freeze', this.onPageLeave);
navigator.mediaDevices?.removeEventListener('devicechange', this.handleDeviceChange);
}
Expand Down
Loading