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();