Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settleConvergence.ts

55 lines2.1 KB

1/**
2 * Watchdog for a settle that the server accepted but the local view never
3 * reflects.
4 *
5 * An accepted `thread.settle` always publishes an authoritative
6 * `thread-upserted`, so the thread should read as settled locally within a
7 * moment. When it does not, the client is working from a view the server no
8 * longer agrees with — and because settling an already-settled thread is a
9 * deliberate idempotent no-op, every further click succeeds while changing
10 * nothing. The control looks dead and the thread cannot be moved to settled at
11 * all until the client is restarted.
12 *
13 * Polling rather than subscribing keeps this out of the atom graph: it runs
14 * only after an explicit user action, and only until the view agrees.
15 */
16​
17/** How long to wait before each re-read. Spaced out rather than uniform: the
18 * common case resolves on the first check, and a slow environment gets a
19 * couple of longer chances before we escalate to a resync. */
20export const SETTLE_CONVERGENCE_DELAYS_MS: readonly number[] = [150, 500, 1200];
21​
22export type SettleConvergenceOutcome =
23 /** The local view caught up on its own; nothing to do. */
24 | "converged"
25 /** The view never caught up, so a reconcile was requested. */
26 | "resync-requested"
27 /** The thread left the shell entirely (deleted, archived); not our problem. */
28 | "thread-absent";
29​
30export interface SettleConvergenceOptions {
31 /** `true` when the local view shows the thread settled, `false` when it does
32 * not, and `null` when the thread is no longer in the local shell. */
33 readonly readSettled: () => boolean | null;
34 readonly requestResync: () => void;
35 readonly delay: (ms: number) => Promise<void>;
36 readonly delaysMs?: readonly number[];
38​
39export async function confirmSettleConverged(
40 options: SettleConvergenceOptions,
41): Promise<SettleConvergenceOutcome> {
42 const delays = options.delaysMs ?? SETTLE_CONVERGENCE_DELAYS_MS;
43 for (const waitMs of delays) {
44 await options.delay(waitMs);
45 const settled = options.readSettled();
46 if (settled === null) {
47 return "thread-absent";
48 }
49 if (settled) {
50 return "converged";
51 }
52 }
53 options.requestResync();
54 return "resync-requested";