Share link: restore() reports false when the hash is not a share state

This commit is contained in:
Sameh Khamis committed 2026-11-16 13:38:42 -08:00
1 parent de0be208da
commit 1fa053e22d
2 files changed
+25 -6

No files matched your search

+10 -6
View File
@@ -361,7 +361,8 @@ export class ShareLinkManager {
* shows: an owner that changed the view under it (Stories, after a GEV
* scene) hands the person's view back this way. Navigation must be watched.
* @param {string} hash Without the leading `#`.
* @returns {boolean} Whether a restore began.
* @returns {boolean} Whether a restore began: false when the hash is not a
* share state, which a later hashchange may then try again.
*/
restore(hash) {
if (this._destroyed || !this._onNavigate || typeof hash !== 'string')
@@ -370,18 +371,20 @@ export class ShareLinkManager {
url.hash = hash;
history.replaceState(history.state, '', url.href);
this._knownHash = null;
this._handleLocationChange(this._onNavigate);
return this._knownHash === hash;
if (this._handleLocationChange(this._onNavigate)) return true;
this._knownHash = null;
return false;
}
/** @returns {boolean} Whether a restore began. */
_handleLocationChange(onNavigate) {
if (this._destroyed) return;
if (this._destroyed) return false;
const hash = window.location.hash.slice(1);
// Back/Forward fires both popstate and hashchange for one entry.
if (hash === this._knownHash) return;
if (hash === this._knownHash) return false;
this._knownHash = hash;
const state = this._parseShareHash(hash);
if (!state) return;
if (!state) return false;
this._beginIncomingRestore(state);
onNavigate(state);
try {
@@ -389,6 +392,7 @@ export class ShareLinkManager {
} catch (error) {
console.warn('[ShareLink] library restore failed:', error);
}
return true;
}
/**
+15
View File
@@ -155,6 +155,21 @@ test('restore applies a hash even when the address already shows it', (t) => {
manager.destroy();
});
test('restore reports a hash that is not a share state as not restored', (t) => {
installTab(t, SHARE_A);
const manager = new ShareLinkManager(bindFakeCesiumCamera(fakeViewer()));
manager.completeInitialRestore(manager.parseInitialHash().restoreToken);
const restored = [];
manager.watchNavigation((state) => restored.push(state));
assert.equal(manager.restore('not-a-share-state'), false);
assert.equal(restored.length, 0);
// The caller's hashchange fallback is not swallowed as already known.
assert.equal(manager._knownHash, null);
assert.equal(manager.restore(SHARE_A), true);
assert.equal(restored.length, 1);
manager.destroy();
});
test('holding writes first writes a view change still waiting for its debounce', (t) => {
installTab(t, SHARE_A);
const timers = installManualTimers(t);