diff --git a/packages/runtime/src/pluginApi.ts b/packages/runtime/src/pluginApi.ts index 735e1cd..d44c866 100644 --- a/packages/runtime/src/pluginApi.ts +++ b/packages/runtime/src/pluginApi.ts @@ -169,6 +169,8 @@ export function createCorePluginApi(win: Window & typeof globalThis, site: Plugi const previous = lastHref; lastHref = href; for (const listener of [...routeListeners]) { + // A listener removed by an earlier one in this round (a feature stopped by the route) is not called. + if (!routeListeners.has(listener)) continue; try { listener(href, previous); } catch (error) { report(error); } } }; @@ -255,6 +257,8 @@ export function createCorePluginApi(win: Window & typeof globalThis, site: Plugi if (!routeTimer) { lastHref = win.location.href; routeTimer = setInterval(checkRoute, ROUTE_POLL_MS); + // Outside a browser (tests) the poll never keeps the process alive. + (routeTimer as { unref?: () => void }).unref?.(); win.addEventListener('popstate', checkRoute); } return () => { diff --git a/packages/runtime/tests/features.test.ts b/packages/runtime/tests/features.test.ts index b64ba5b..d167d66 100644 --- a/packages/runtime/tests/features.test.ts +++ b/packages/runtime/tests/features.test.ts @@ -226,6 +226,22 @@ test('registry: a module runs only while its flag is on and the page is on one o registry.dispose(); }); +test('registry: a feature stopped by a route change is not told about that route (nothing re-added after stop)', () => { + const env = page(); + const registry = registryFor(env, { surfaces: () => (env.location.pathname === '/someone' ? ['profile'] : ['timeline']) }); + const routes: string[] = []; + registry.define({ id: 'hideTrends', start(ctx) { ctx.css('.trends{display:none}'); ctx.onRouteChange(href => { routes.push(href); ctx.css('.trends{opacity:0}'); }); } }); + registry.setFlags(effectiveFlags(TABLE, { ...tableDefaults(TABLE), hideTrends: true })); + env.go('/explore'); + assert.deepEqual(routes.length, 1, 'still on a timeline: the feature hears the route'); + assert.deepEqual(env.liveStyles(), ['.trends{opacity:0}']); + env.go('/someone'); + assert.deepEqual(registry.active(), []); + assert.equal(routes.length, 1, 'stopped in this round: its listener is not called'); + assert.deepEqual(env.liveStyles(), []); + registry.dispose(); +}); + test('registry: one MutationObserver however many features observe; plugins share it too', () => { const env = page(); const registry = registryFor(env);