Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
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
24 changes: 22 additions & 2 deletions src/utils/sseMultiplexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,29 @@ class SseMultiplexer {
this.queryGlobalSubscribers();
this.reconcileConnections();

// Keep the lock active until tab unloads/unmounts
// Detect when the tab is backgrounded so the lock is released cleanly
// before the browser auto-releases it (which would leave releaseLockPromise hanging).
const onVisibilityChange = () => {
if (document.visibilityState === "hidden") {
this.stopHeartbeatChecks();
this.isLeader = false;
document.removeEventListener("visibilitychange", onVisibilityChange);
if (this.releaseLockPromise) {
this.releaseLockPromise();
this.releaseLockPromise = null;
}
// Revert to localStorage-based election so another tab can claim leadership
this.setupLocalStorageElection();
}
};
document.addEventListener("visibilitychange", onVisibilityChange);

// Keep the lock active until tab unloads/unmounts or is backgrounded
await new Promise((resolve) => {
this.releaseLockPromise = resolve;
this.releaseLockPromise = () => {
document.removeEventListener("visibilitychange", onVisibilityChange);
resolve();
};
});
})
.catch((err) => {
Expand Down
Loading