Handle pagehide and visibilitychange events in safari

This commit is contained in:
Robert Long 2021-08-09 17:44:26 -07:00
parent 323808f495
commit 8554184beb

View File

@ -161,9 +161,12 @@ export function useVideoRoom(manager, roomId, timeout = 5000) {
}
// iOS doesn't fire beforeunload event, so leave the call when you hide the page.
const unloadEvent = isIOS() ? "visibilitychange" : "beforeunload";
if (isIOS()) {
window.addEventListener("pagehide", onBeforeUnload);
document.addEventListener("visibilitychange", onBeforeUnload);
}
window.addEventListener(unloadEvent, onBeforeUnload);
window.addEventListener("beforeunload", onBeforeUnload);
const onParticipantsChanged = () => {
setState((prevState) => ({
@ -219,7 +222,9 @@ export function useVideoRoom(manager, roomId, timeout = 5000) {
return () => {
manager.client.removeListener("Room", roomCallback);
manager.removeListener("participants_changed", onParticipantsChanged);
window.removeEventListener(unloadEvent, onBeforeUnload);
window.removeEventListener("pagehide", onBeforeUnload);
document.removeEventListener("visibilitychange", onBeforeUnload);
window.removeEventListener("beforeunload", onBeforeUnload);
clearTimeout(timeoutId);
manager.leaveCall();
};