diff options
| author | Adam Malczewski <[email protected]> | 2026-06-01 10:41:47 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-01 10:41:47 +0900 |
| commit | ecce93d9dfecd0c61c0f8c3fa8024f60359e1f17 (patch) | |
| tree | 81ea46a48b2c67ec3bba74102fd80334c0e63409 /packages/frontend/src/lib | |
| parent | dbabca525ee70126b3d2264dad88c0dcca630b83 (diff) | |
| download | dispatch-ecce93d9dfecd0c61c0f8c3fa8024f60359e1f17.tar.gz dispatch-ecce93d9dfecd0c61c0f8c3fa8024f60359e1f17.zip | |
feat(frontend): SnapshotSequencer — reusable 'most-recent request wins' race guard
Tiny, dependency-free class for the common pattern where a component
fans out multiple HTTP calls that each return a full snapshot of
shared state, and applying an older snapshot would clobber a newer
one. begin() tags a new request, accept(seq) decides whether to
apply the response.
Pulled out as its own module (rather than inlined in ClaudeReset)
because the next consumer of this pattern shouldn't have to
re-derive it. The contract is small enough to test exhaustively
in isolation:
- accepts the first response unconditionally
- accepts responses in send order
- rejects an older response that arrives AFTER a newer one (the
core race that motivated this)
- rejects ALL stragglers once a newer one wins
- handles the initial-load vs first-click race
- equal seq is idempotent accept (defensive)
- begin() seqs are monotonic and unique
- state inspector reflects the watermark
8 tests, all green. No Svelte dependency — usable from any TS file.
Diffstat (limited to 'packages/frontend/src/lib')
| -rw-r--r-- | packages/frontend/src/lib/snapshot-sequencer.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/frontend/src/lib/snapshot-sequencer.ts b/packages/frontend/src/lib/snapshot-sequencer.ts new file mode 100644 index 0000000..fccc9ef --- /dev/null +++ b/packages/frontend/src/lib/snapshot-sequencer.ts @@ -0,0 +1,47 @@ +/** + * Tiny race guard for "the most-recent request wins" semantics. + * + * When a frontend component fans out multiple HTTP calls that each return a + * full snapshot of shared state — and applying an older snapshot would clobber + * a newer one — wrap each call with `seq = sequencer.begin()` before send and + * `sequencer.accept(seq)` before applying the response. Older sequences are + * rejected. + * + * Why: the Claude Wake Schedule's POST /toggle and GET /wake-schedule both + * return the *whole* schedule. If a user toggles hour 9 (request A) and then + * hour 10 (request B), and B's response arrives before A's, the older A + * response — which doesn't know about hour 10 yet — would otherwise overwrite + * hour 10 right out of the UI. A per-hour counter is NOT enough because the + * race spans different hours (and also covers the initial-load vs first-click + * race). + * + * `>=` on accept is intentional: if seq equals the latest applied seq, the + * response is a redundant arrival of the most-recent winner — accepting it + * (idempotently) is fine. The discriminator is *strictly less than*. + */ +export class SnapshotSequencer { + private nextSeq = 0; + private latestApplied = 0; + + /** Tag a new request. Call before sending; pass the returned seq to accept(). */ + begin(): number { + this.nextSeq += 1; + return this.nextSeq; + } + + /** + * Decide whether to apply a response. Returns true if this seq is the + * newest seen so far (and updates the watermark); false if a newer + * response has already won. + */ + accept(seq: number): boolean { + if (seq < this.latestApplied) return false; + this.latestApplied = seq; + return true; + } + + /** Inspect (for tests / debugging). */ + get state(): { nextSeq: number; latestApplied: number } { + return { nextSeq: this.nextSeq, latestApplied: this.latestApplied }; + } +} |
