jsdoc typing round, phases 3-4: the clerk (tool/memory.mjs) and the SPA (web/) typed under strict -- row shapes local to their owners (the clerk's five tables in its header; the SPA's response envelopes in the new web/shapes.mjs, the server's shared shapes imported at their use sites: User, LogEvent, JobState, HttpError); the db boundary cast at every .get()/.all(), fail as @returns {never} so guard-narrowing works; web via a second tsconfig (tsconfig.web.json: DOM libs in their own program, node types riding along because shapes.mjs's Provider typedef pulls provider.mjs in), npm run typecheck runs both; every non-annotation delta audited onto a small intended list (COUNT(*) Number() wraps, impossible-path guards, ?? ''/?? undefined null-absorbers, the RewordOp reason fix -- the validator demands it, the reminder-find naming its tag); smoke 110/110 + restart + reminder + repo checks + a zero-console-error browser pass green; the typing round is complete
This commit is contained in:
+57
-34
@@ -3,6 +3,13 @@
|
||||
// instances), the GM notepad, housekeeping (close session, the
|
||||
// dry-run replay), fork, and the input line.
|
||||
|
||||
/** @typedef {import('./shapes.mjs').RunDetail} RunDetail */
|
||||
/** @typedef {import('./shapes.mjs').TableResponse} TableResponse */
|
||||
/** @typedef {import('./shapes.mjs').CarryoverResponse} CarryoverResponse */
|
||||
/** @typedef {import('./shapes.mjs').NotesResponse} NotesResponse */
|
||||
/** @typedef {import('./shapes.mjs').HousekeepingLogResponse} HousekeepingLogResponse */
|
||||
/** @typedef {import('../server/shapes.mjs').JobState} JobState */
|
||||
|
||||
import { $, el } from './dom.mjs';
|
||||
import { api } from './client.mjs';
|
||||
import { state } from './state.mjs';
|
||||
@@ -49,23 +56,26 @@ const models = modelsBox('models-pane', { title: 'models' });
|
||||
export function leave() {
|
||||
saveNotes();
|
||||
stopPoll();
|
||||
$('ask-modal').close();
|
||||
/** @type {HTMLDialogElement} */ ($('ask-modal')).close();
|
||||
}
|
||||
|
||||
/** @param {?string} id @param {number} seq @returns {Promise<void>} */
|
||||
export async function enter(id, seq) {
|
||||
if (id == null) return; // unreachable: the router only enters play with a run id
|
||||
statusPane.reset();
|
||||
agendaPane.reset();
|
||||
$('conflict').hidden = true;
|
||||
await saveNotes(); // flush the pad for the previous run before switching
|
||||
state.runId = id;
|
||||
notesDirty = false;
|
||||
$('gm-notes').value = '';
|
||||
/** @type {HTMLTextAreaElement} */ ($('gm-notes')).value = '';
|
||||
$('notes-state').textContent = '';
|
||||
await refreshPlay(id, seq);
|
||||
}
|
||||
|
||||
/** @param {string} id @param {number} seq @returns {Promise<void>} */
|
||||
async function refreshPlay(id, seq) {
|
||||
let detail;
|
||||
/** @type {RunDetail} */ let detail;
|
||||
try {
|
||||
detail = await api(`/runs/${id}`);
|
||||
} catch (e) {
|
||||
@@ -73,7 +83,7 @@ async function refreshPlay(id, seq) {
|
||||
return banner(`could not reach the server: ${e.message}`); // stay put -- transient
|
||||
}
|
||||
if (seq !== state.seq) return;
|
||||
const mine = state.me && detail.run.owner === state.me.id;
|
||||
const mine = !!(state.me && detail.run.owner === state.me.id);
|
||||
if (detail.draft) {
|
||||
if (mine) return navigate(`/runs/${id}/setup`);
|
||||
// someone else's unstarted run: nothing to read but its existence
|
||||
@@ -96,19 +106,19 @@ async function refreshPlay(id, seq) {
|
||||
// way in. Ownerless runs are spectators for everyone alike.
|
||||
statusPane.setEditable(mine);
|
||||
agendaPane.setEditable(mine);
|
||||
$('gm-notes').readOnly = !mine;
|
||||
/** @type {HTMLTextAreaElement} */ ($('gm-notes')).readOnly = !mine;
|
||||
|
||||
if (detail.phase === 'live') {
|
||||
// the table: the live session's projection -- thinking never
|
||||
// crosses the wire, variant turns carry only the picked one (the
|
||||
// raw /session is the audit surface, one fetch away)
|
||||
const session = await api(`/runs/${id}/table`);
|
||||
/** @type {TableResponse} */ const session = await api(`/runs/${id}/table`);
|
||||
if (seq !== state.seq) return;
|
||||
renderTranscript(session.entries, { onConflict: showConflict });
|
||||
} else if (detail.phase === 'dormant') {
|
||||
// the review window: the carryover is the preview of the next
|
||||
// contwin's opening, rendered exactly as the table will hold it
|
||||
const carry = await api(`/runs/${id}/carryover`).catch(() => null);
|
||||
/** @type {?CarryoverResponse} */ const carry = await api(`/runs/${id}/carryover`).catch(() => null);
|
||||
if (seq !== state.seq) return;
|
||||
$('transcript').textContent = '';
|
||||
if (carry && carry.entries.length) renderTranscript(carry.entries, {});
|
||||
@@ -126,7 +136,7 @@ async function refreshPlay(id, seq) {
|
||||
$('hk-replay').hidden = !((detail.phase === 'live') || (detail.phase === 'dormant' && detail.sessions >= 2)) || !mine;
|
||||
$('hk-override-row').hidden = !(detail.phase === 'dormant' && detail.ready === false) || !mine;
|
||||
setInput(detail.phase === 'live' && !detail.pendingAsk && mine);
|
||||
$('gm-input').placeholder = mine
|
||||
/** @type {HTMLInputElement} */ ($('gm-input')).placeholder = mine
|
||||
? 'GM line -- Enter sends, Shift+Enter newlines'
|
||||
: 'spectating -- fork this run to continue it as yours';
|
||||
|
||||
@@ -142,14 +152,14 @@ async function refreshPlay(id, seq) {
|
||||
// to answer; a spectator follows the thread in the transcript
|
||||
if (detail.pendingAsk && mine) {
|
||||
$('ask-question').textContent = detail.pendingAsk;
|
||||
$('ask-modal').showModal();
|
||||
$('ask-answer').focus();
|
||||
/** @type {HTMLDialogElement} */ ($('ask-modal')).showModal();
|
||||
/** @type {HTMLInputElement} */ ($('ask-answer')).focus();
|
||||
} else {
|
||||
$('ask-modal').close();
|
||||
/** @type {HTMLDialogElement} */ ($('ask-modal')).close();
|
||||
}
|
||||
|
||||
// housekeeping state
|
||||
const hk = await api(`/runs/${id}/housekeeping`);
|
||||
/** @type {JobState} */ const hk = await api(`/runs/${id}/housekeeping`);
|
||||
if (seq !== state.seq) return;
|
||||
renderHk(hk);
|
||||
|
||||
@@ -159,6 +169,7 @@ async function refreshPlay(id, seq) {
|
||||
|
||||
// the dormant bar: the review window's banner over the carryover
|
||||
// preview, with the one button that leaves it
|
||||
/** @param {RunDetail} detail @param {?CarryoverResponse} carry @returns {HTMLElement} */
|
||||
function dormantBar(detail, carry) {
|
||||
const bar = el('div', 'beat');
|
||||
const ending = carry?.ends === 'gm'
|
||||
@@ -171,6 +182,7 @@ function dormantBar(detail, carry) {
|
||||
return bar;
|
||||
}
|
||||
|
||||
/** @param {string} text */
|
||||
function showConflict(text) {
|
||||
const c = $('conflict');
|
||||
c.textContent = `conflict: ${text}`;
|
||||
@@ -183,26 +195,28 @@ function showConflict(text) {
|
||||
// flushed when leaving the play view or switching runs. gm.md never
|
||||
// reaches any model -- this is the GM's own notes.
|
||||
|
||||
let notesTimer = null;
|
||||
/** @type {?ReturnType<typeof setTimeout>} */ let notesTimer = null;
|
||||
let notesDirty = false;
|
||||
|
||||
/** @param {string} id @param {number} seq @returns {Promise<void>} */
|
||||
async function loadNotes(id, seq) {
|
||||
const notes = await api(`/runs/${id}/notes`).catch(() => null);
|
||||
/** @type {?NotesResponse} */ const notes = await api(`/runs/${id}/notes`).catch(() => null);
|
||||
if (!notes || seq !== state.seq || notesDirty) return;
|
||||
$('gm-notes').value = notes.content;
|
||||
/** @type {HTMLTextAreaElement} */ ($('gm-notes')).value = notes.content;
|
||||
}
|
||||
|
||||
function notesInput() {
|
||||
notesDirty = true;
|
||||
$('notes-state').textContent = 'unsaved';
|
||||
clearTimeout(notesTimer);
|
||||
clearTimeout(notesTimer ?? undefined);
|
||||
notesTimer = setTimeout(saveNotes, 1000);
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function saveNotes() {
|
||||
clearTimeout(notesTimer);
|
||||
clearTimeout(notesTimer ?? undefined);
|
||||
if (!notesDirty || !state.runId) return;
|
||||
const content = $('gm-notes').value;
|
||||
const content = /** @type {HTMLTextAreaElement} */ ($('gm-notes')).value;
|
||||
try {
|
||||
// keepalive: the flush must survive leaving the page entirely
|
||||
await api(`/runs/${state.runId}/notes`, { method: 'PUT', body: { content }, keepalive: true });
|
||||
@@ -216,13 +230,14 @@ async function saveNotes() {
|
||||
|
||||
// start: the dormant session goes live. If the carryover ends on a GM
|
||||
// turn, the server sends it automatically as her cue.
|
||||
/** @returns {Promise<void>} */
|
||||
async function startRun() {
|
||||
const override = $('hk-override').checked;
|
||||
const override = /** @type {HTMLInputElement} */ ($('hk-override')).checked;
|
||||
hideBanner();
|
||||
setBusy(true, 'starting...');
|
||||
try {
|
||||
const r = await api(`/runs/${state.runId}/sessions`, { method: 'POST', body: { override } });
|
||||
$('hk-override').checked = false;
|
||||
/** @type {HTMLInputElement} */ ($('hk-override')).checked = false;
|
||||
if (r.autoSend) logEvent('nav', 'the carryover ended on a GM turn -- sent automatically as her cue');
|
||||
} catch (e) {
|
||||
if (e.status === 409 && e.detail?.hint) {
|
||||
@@ -237,12 +252,14 @@ async function startRun() {
|
||||
render();
|
||||
}
|
||||
|
||||
/** @param {string} stateText @param {?string} [cls] */
|
||||
function setHk(stateText, cls) {
|
||||
const h = $('hk-state');
|
||||
h.textContent = stateText;
|
||||
h.className = cls ?? '';
|
||||
}
|
||||
|
||||
/** @param {JobState} hk */
|
||||
function renderHk(hk) {
|
||||
if (hk.state === 'running') {
|
||||
setHk(hk.phase === 'rerun' ? 're-running housekeeping...' : 'stopping & housekeeping...', '');
|
||||
@@ -262,39 +279,42 @@ function renderHk(hk) {
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {boolean} ok */
|
||||
function setInput(ok) {
|
||||
$('gm-input').disabled = !ok;
|
||||
$('gm-send').disabled = !ok;
|
||||
/** @type {HTMLInputElement} */ ($('gm-input')).disabled = !ok;
|
||||
/** @type {HTMLButtonElement} */ ($('gm-send')).disabled = !ok;
|
||||
}
|
||||
|
||||
// --- actions -----------------------------------------------------------
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function sendTurn() {
|
||||
const line = $('gm-input').value.trim();
|
||||
const line = /** @type {HTMLInputElement} */ ($('gm-input')).value.trim();
|
||||
if (!line || !state.runId) return;
|
||||
$('gm-input').value = '';
|
||||
/** @type {HTMLInputElement} */ ($('gm-input')).value = '';
|
||||
setInput(false);
|
||||
setBusy(true);
|
||||
try {
|
||||
await api(`/runs/${state.runId}/turn`, { method: 'POST', body: { line } });
|
||||
} catch (e) {
|
||||
banner(e.message);
|
||||
$('gm-input').value = line; // a failed send must not eat the line
|
||||
/** @type {HTMLInputElement} */ ($('gm-input')).value = line; // a failed send must not eat the line
|
||||
}
|
||||
setBusy(false);
|
||||
render();
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function sendAnswer() {
|
||||
const answer = $('ask-answer').value.trim();
|
||||
const answer = /** @type {HTMLInputElement} */ ($('ask-answer')).value.trim();
|
||||
if (!answer || !state.runId) return;
|
||||
$('ask-answer').value = '';
|
||||
/** @type {HTMLInputElement} */ ($('ask-answer')).value = '';
|
||||
setBusy(true);
|
||||
try {
|
||||
await api(`/runs/${state.runId}/ask`, { method: 'POST', body: { answer } });
|
||||
} catch (e) {
|
||||
banner(e.message);
|
||||
$('ask-answer').value = answer;
|
||||
/** @type {HTMLInputElement} */ ($('ask-answer')).value = answer;
|
||||
}
|
||||
setBusy(false);
|
||||
render();
|
||||
@@ -302,6 +322,7 @@ async function sendAnswer() {
|
||||
|
||||
// fork: a copy of this run under you -- any run, anyone's, including
|
||||
// yours. Lands you in the fork.
|
||||
/** @returns {Promise<void>} */
|
||||
async function forkRun() {
|
||||
if (!state.runId) return;
|
||||
setBusy(true, 'forking...');
|
||||
@@ -318,6 +339,7 @@ async function forkRun() {
|
||||
|
||||
// dry-run: the housekeeper reads the session log, the manifest lands
|
||||
// in the pane -- nothing applied, the session stays open
|
||||
/** @returns {Promise<void>} */
|
||||
async function replayHousekeeping() {
|
||||
setBusy(true, 'the housekeeper reads...');
|
||||
try {
|
||||
@@ -325,8 +347,8 @@ async function replayHousekeeping() {
|
||||
$('hk-replay-content').textContent = r.parsed
|
||||
? JSON.stringify(r.manifest, null, 2)
|
||||
: `${r.reason}\n\nraw output: ${r.file}`;
|
||||
$('hk-replay-log').hidden = false;
|
||||
$('hk-replay-log').open = true;
|
||||
/** @type {HTMLDetailsElement} */ ($('hk-replay-log')).hidden = false;
|
||||
/** @type {HTMLDetailsElement} */ ($('hk-replay-log')).open = true;
|
||||
} catch (e) {
|
||||
banner(e.message);
|
||||
}
|
||||
@@ -335,7 +357,7 @@ async function replayHousekeeping() {
|
||||
|
||||
// --- housekeeping poll + log -------------------------------------------
|
||||
|
||||
let poll = null;
|
||||
/** @type {?ReturnType<typeof setInterval>} */ let poll = null;
|
||||
|
||||
function startPoll() {
|
||||
stopPoll();
|
||||
@@ -350,12 +372,13 @@ function stopPoll() {
|
||||
poll = null;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function loadLog() {
|
||||
try {
|
||||
const log = await api(`/runs/${state.runId}/housekeeping/log`);
|
||||
/** @type {HousekeepingLogResponse} */ const log = await api(`/runs/${state.runId}/housekeeping/log`);
|
||||
$('hk-log-content').textContent = log.content;
|
||||
$('hk-log').hidden = false;
|
||||
$('hk-log').open = true;
|
||||
/** @type {HTMLDetailsElement} */ ($('hk-log')).hidden = false;
|
||||
/** @type {HTMLDetailsElement} */ ($('hk-log')).open = true;
|
||||
} catch {
|
||||
$('hk-log').hidden = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user