import { readInbox } from "./inbox"; import { inboxHeadline } from "./inbox/headline.mjs"; import { machineCalls } from "./machine"; import { listPieces } from "./pieces"; // The rail's two counts (canvas rail: "Inbox 7", "Work 14"): the calls waiting // (lib/inbox/headline.mjs, plus the machine's) and the active pieces. Every // page's rail asks for them after it loads (components/shell/RailCount.tsx), so // they are memoised for a few seconds -- on globalThis, so every bundle shares // one copy (the P2a project-cache lesson) -- and cleared by a lifecycle write. // SERVER ONLY. export type RailCounts = { inbox: number; work: number }; const MEMO_MS = 10_000; const KEY = Symbol.for("umtool.rail-counts"); type Memo = { at: number; value: Promise } | null; const store = globalThis as unknown as Record; export function clearRailCounts(): void { store[KEY] = null; } export function railCounts(): Promise { const m = store[KEY]; if (m && Date.now() - m.at < MEMO_MS) return m.value; const value = (async () => { const [inbox, pieces, machine] = await Promise.all([readInbox(), listPieces(), machineCalls()]); return { inbox: inboxHeadline(inbox.groups).calls + machine.length, // Work's rows: an edition's article is its series' row (R5). work: pieces.filter((p) => p.lifecycle === "active" && !p.series).length, }; })(); const entry = { at: Date.now(), value }; store[KEY] = entry; value.catch(() => { if (store[KEY] === entry) store[KEY] = null; }); return value; }