Skip to content
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

Fix Safari multiple tabs by working around a Safari bug. #7316

Merged
merged 1 commit into from Oct 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/apputils/src/windowresolver.ts
Expand Up @@ -84,6 +84,18 @@ namespace Private {
*/
const WINDOW = `${PREFIX}:window`;

/**
* Current beacon request
*
* #### Notes
* We keep track of the current request so that we can ignore our own beacon
* requests. This is to work around a bug in Safari, where Safari sometimes
* triggers local storage events for changes made by the current tab. See
* https://github.com/jupyterlab/jupyterlab/issues/6921#issuecomment-540817283
* for more details.
*/
let currentBeaconRequest: string | null = null;

/**
* A potential preferred default window name.
*/
Expand Down Expand Up @@ -123,7 +135,11 @@ namespace Private {
}

// If the beacon was fired, respond with a ping.
if (key === BEACON && candidate !== null) {
if (
key === BEACON &&
newValue !== currentBeaconRequest &&
candidate !== null
) {
ping(resolved ? name : candidate);
return;
}
Expand Down Expand Up @@ -163,6 +179,7 @@ namespace Private {
*/
function reject(): void {
resolved = true;
currentBeaconRequest = null;
delegate.reject(`Window name candidate "${candidate}" already exists`);
}

Expand Down Expand Up @@ -197,12 +214,14 @@ namespace Private {
}

resolved = true;
currentBeaconRequest = null;
delegate.resolve((name = candidate));
ping(name);
}, TIMEOUT);

// Fire the beacon to collect other windows' names.
localStorage.setItem(BEACON, `${Math.random()}-${new Date().getTime()}`);
currentBeaconRequest = `${Math.random()}-${new Date().getTime()}`;
localStorage.setItem(BEACON, currentBeaconRequest);

return delegate.promise;
}
Expand Down