Route listeners: one removed by an earlier listener in the same round is not called (a feature stopped by the route re-added its stylesheet); the route poll never keeps a test process alive

This commit is contained in:
CRTkafa 2026-09-30 21:19:02 +07:00
parent 1dad3d294a
commit 6e80b3e2c9
2 changed files with 20 additions and 0 deletions

View file

@ -169,6 +169,8 @@ export function createCorePluginApi(win: Window & typeof globalThis, site: Plugi
const previous = lastHref; const previous = lastHref;
lastHref = href; lastHref = href;
for (const listener of [...routeListeners]) { 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); } try { listener(href, previous); } catch (error) { report(error); }
} }
}; };
@ -255,6 +257,8 @@ export function createCorePluginApi(win: Window & typeof globalThis, site: Plugi
if (!routeTimer) { if (!routeTimer) {
lastHref = win.location.href; lastHref = win.location.href;
routeTimer = setInterval(checkRoute, ROUTE_POLL_MS); 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); win.addEventListener('popstate', checkRoute);
} }
return () => { return () => {

View file

@ -226,6 +226,22 @@ test('registry: a module runs only while its flag is on and the page is on one o
registry.dispose(); 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', () => { test('registry: one MutationObserver however many features observe; plugins share it too', () => {
const env = page(); const env = page();
const registry = registryFor(env); const registry = registryFor(env);