/** * The bus as a reader sees it: the rows a page or `session status` draws, and the numbers on them. * * One rule needs to live in one place: the number is the mail a session has not been told about, * so the row for the reading session comes from memory — its record on disk is the copy a peer * reads, and that copy lags the mail it was just told about — while every other row is read from * the file, because the file is all there is. A session's own count therefore falls in the call * that announced its mail; a peer's is one heartbeat behind, which is the same window its row * already has. * * The read writes nothing, so a board can ask every second: `liveOnly` only filters what it draws, * and the prune stays with the next tool or command call. */ import { readMailLedger, unaccountedOf, untoldIn } from "../../lib/session-mail.ts"; import type { RosterState } from "../../lib/session-roster.ts"; import type { WatchingState } from "./mail-watch.ts"; import type { BusStore } from "./store.ts"; import type { SessionRecord, SessionStatus } from "./types.ts"; interface StatusReaderOptions { store(): BusStore | undefined; /** This session's own record, live in memory rather than as the file holds it. */ record(): SessionRecord | undefined; /** How one record reads at a moment: live, quiet, or gone. */ stateOf(session: SessionRecord, at?: number): RosterState; now(): number; /** The watch as this session has it; whether it was armed is the reader's to decide. */ watching(): WatchingState; } interface StatusReader { /** * The bus as it stands, this session's own record included. * * `liveOnly` drops a session whose heartbeat went stale, and the claims of one whose process is * gone, without writing or deleting anything, so a board can read it every second. A gone * session stays listed — a lead needs a face for the paths it still holds — but its claims are * nobody's, so they are not drawn. */ read(me: { id: string; label: string; onBus: boolean }, liveOnly: boolean): Promise; } export function createStatusReader(options: StatusReaderOptions): StatusReader { const { store, record, stateOf, now, watching } = options; /** One row per record, each counting the mail that record has not been told about. */ function rowsOf( rows: readonly SessionRecord[], untoldOf: (id: string) => number, ): Array { const at = now(); const mine = record(); return rows.map((row) => { const session = mine !== undefined && row.id === mine.id ? mine : row; return { ...session, unread: untoldOf(session.id), state: stateOf(session, at) }; }); } return { async read(me, liveOnly) { const bus = store(); if (!bus) throw new Error("session bus is not started."); const records = await bus.sessions(); const sessions = liveOnly ? records.filter((session) => session.id === record()?.id || stateOf(session) !== "quiet") : records; const ours = new Set(sessions.filter((session) => stateOf(session) !== "gone").map((session) => session.id)); const claims = (await bus.claims()).filter((claim) => !liveOnly || ours.has(claim.owner)); // One pass answers every mail question this read has: each row's count, the mail in the // inboxes no record holds, and the files this build cannot parse. Mail is counted per // record, so a pruned record's messages would have no row to hang a number on — the ids // read here are the *record* ids rather than the live ones, because a gone record's mail // is still its own. The reader's own record is the live one, so its count falls the moment // its mail is announced rather than one heartbeat later. const mine = record(); const mail = await readMailLedger( bus.dir, records.map((session) => { const live = mine !== undefined && session.id === mine.id ? mine : session; return { id: live.id, label: live.label, told: live.seenMail ?? [] }; }), ); const unaccounted = unaccountedOf(mail); // The one thing a count cannot say: a file this build cannot parse sits inside an inbox // until its TTL. The pass above named them, so no second read is asked for. const refused = liveOnly ? [] : (mail.inboxes.find((inbox) => inbox.id === me.id)?.unreadable ?? []); const watch = watching(); return { me: { id: me.id, label: me.label }, onBus: me.onBus, sessions: rowsOf(sessions, (id) => untoldIn(mail, id)), ...(unaccounted.count > 0 ? { unaccounted } : {}), ...(refused.length === 0 ? {} : { unreadable: refused }), claims, ...(watch.armed ? { watching: watch } : {}), }; }, }; }