spa modules and terminology: app.mjs split into flat es modules (router lifecycle, shared widgets, views own their state), mock smoke in test/; DICTIONARY.md rules the terms, four renames swept clean through the machinery - paper->status (run summary command is report now), window->session, wrap->housekeeping, context window->contwin; no dual-accepts by ruling, old data migrates once in the clerk (restore self-heals old dumps)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// web/router.mjs — path routing and the view lifecycle.
|
||||
//
|
||||
// Views register { el, enter, leave? } at boot (app.mjs). render()
|
||||
// routes the path, then show() runs the previous view's leave and
|
||||
// the next view's enter — each view owns its own teardown, so the
|
||||
// router never has to know a pane's internals. Stale async work is
|
||||
// dropped by the seq guard: a view captures seq at enter and checks
|
||||
// it after every await.
|
||||
|
||||
import { $ } from './dom.mjs';
|
||||
import { state } from './state.mjs';
|
||||
import { banner, hideBanner, renderMeter, setWho, setSessionInfo } from './chrome.mjs';
|
||||
import { logEvent } from './log.mjs';
|
||||
|
||||
const views = {};
|
||||
export function registerViews(map) { Object.assign(views, map); }
|
||||
|
||||
export function navigate(p) {
|
||||
history.pushState({}, '', p);
|
||||
render();
|
||||
}
|
||||
|
||||
window.addEventListener('popstate', () => render());
|
||||
|
||||
export async function render() {
|
||||
const seq = ++state.seq;
|
||||
hideBanner();
|
||||
const p = location.pathname;
|
||||
let name = null;
|
||||
let arg = null;
|
||||
let m;
|
||||
if (p === '/') name = 'home';
|
||||
else if (p === '/new') name = 'new';
|
||||
else if ((m = p.match(/^\/runs\/([a-z0-9-]+)\/setup$/))) { name = 'setup'; arg = m[1]; }
|
||||
else if ((m = p.match(/^\/runs\/([a-z0-9-]+)$/))) { name = 'play'; arg = m[1]; }
|
||||
else return navigate('/');
|
||||
if (!state.me) name = 'claim'; // signed out: every path leads to the claim
|
||||
await show(name, arg, seq);
|
||||
}
|
||||
|
||||
async function show(name, arg, seq) {
|
||||
const prev = state.view;
|
||||
if (prev && prev !== name && views[prev].leave) views[prev].leave();
|
||||
state.view = name;
|
||||
for (const [n, v] of Object.entries(views)) $(v.el).hidden = n !== name;
|
||||
document.body.classList.toggle('play', name === 'play');
|
||||
setWho(state.me);
|
||||
setSessionInfo('');
|
||||
renderMeter(null);
|
||||
await views[name].enter(arg, seq);
|
||||
}
|
||||
|
||||
// a run that vanished under us (deleted, 404): leave loudly for home.
|
||||
// render() clears the banner first — set it after the navigate.
|
||||
export function runGone(id, from) {
|
||||
navigate('/');
|
||||
banner(`run ${id} is gone — deleted?`);
|
||||
logEvent('nav', `left ${from} for home: run gone (404)`);
|
||||
}
|
||||
Reference in New Issue
Block a user