From 0f13c0a1c2d933903145db2e8461084b32e9cbef Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Thu, 12 Nov 2026 15:49:11 -0800 Subject: [PATCH] Let a newer share switch off a library layer still switching on A share restore skipped every entry whose settled state already matched the share, but a layer still switching on for an earlier share reads as off until it settles. Opening a second share in that window left the first share's layer to come on anyway. The restore now compares with the manager's effective visibility (the newest request), so the newer share supersedes the earlier one. --- src/library/controller.js | 7 ++++++- src/library/controller.test.mjs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/library/controller.js b/src/library/controller.js index b90654c7b..9e2807e4a 100644 --- a/src/library/controller.js +++ b/src/library/controller.js @@ -204,7 +204,12 @@ export function createDataLibrary({ if (wanted && !register(entry)) continue; if (!isRegistered(id)) continue; const layerId = libraryLayerId(id); - if (wanted === (dataManager.isEnabled?.(layerId) === true)) continue; + // Compare with the newest request, not settled state: a layer still + // switching on for an earlier share must be switched off by this one. + const current = + dataManager.isEffectivelyEnabled?.(layerId) ?? + dataManager.isEnabled?.(layerId); + if (wanted === (current === true)) continue; dataManager .setEnabled(layerId, wanted, { origin }) .catch((error) => diff --git a/src/library/controller.test.mjs b/src/library/controller.test.mjs index 53e553233..4b75e7b91 100644 --- a/src/library/controller.test.mjs +++ b/src/library/controller.test.mjs @@ -316,6 +316,35 @@ test('navigating between shares switches library entries on and off', async (t) assert.equal(shareLink.listener, null); }); +test('a share opened while an earlier one is still switching on wins', async (t) => { + const manager = sealedManager(t); + const shareLink = fakeShareLink(null); + const library = createDataLibrary({ + entries, + ledger, + dataManager: manager, + services: {}, + storage: memoryStorage(), + shareLink, + source: source({}), + }); + await library.start(); + const settle = () => + Promise.all( + ['lib-night-lights', 'lib-sea-marks'].map((id) => + manager.waitForLayerSettled?.(id), + ), + ); + // Neither share waits for the one before it to settle. + shareLink.listener(['00']); + assert.equal(manager.isEnabled('lib-night-lights'), false, 'still enabling'); + shareLink.listener(['01']); + await settle(); + assert.equal(manager.isEnabled('lib-night-lights'), false); + assert.equal(manager.isEnabled('lib-sea-marks'), true); + library.destroy(); +}); + test('zoom flies to an added layer through the caller, reset removes everything', async (t) => { const manager = sealedManager(t); const storage = memoryStorage();