Phase 2: TanStack DB over a fake API #3
main fromphase-2-data·merged by fuko Phase 2 ("Data") of trytanstackdb.com V2. The Notes app now runs on TanStack DB query collections over an in-memory fake API, with optimistic mutations and rollbacks.
What's in it#
Fake API (
src/api/):fakeFetch(url, init)returns a realResponsefrom an in-memory REST server seeded from the fixtures. It has endpoints for folders, notes and the account, plus server-side validation.- Latency, fail-next-request and a random failure rate live in a small observable store. The default latency is 500 ms, so optimistic state and rollbacks are visible.
- Every request goes into a bounded request log (200 entries) for the Phase 4 inspector, including malformed requests, which get a logged 400.
Collections (
src/db/collections.ts):foldersCollection,notesCollectionandaccountCollection, built withqueryCollectionOptions.- Handlers send each record's writes in order through a small per-record queue (
src/db/write-queue.ts). - They write the server's response back as confirmed data instead of refetching.
- Handlers send each record's writes in order through a small per-record queue (
Live queries (
src/db/queries.ts) drive every view: folder counts, note lists (smart folders, pinned, date groups), the editor, the account block, settings and the tab title.Mutations (
src/db/mutations.ts):- The editor is editable on macOS and iPhone, with paced saves via
usePacedMutations. - Compose, pin, move, delete/recover/permanent delete and folder rename all work. The rename dialog uses TanStack Form.
- Settings save goes through
accountCollection.
- The editor is editable on macOS and iPhone, with paced saves via
Rollbacks: the server rejects these with real HTTP errors:
- duplicate folder names (409)
- renaming built-in folders (403)
- empty or overlong titles and an invalid email (422)
The optimistic change rolls back and a toast shows the server's message.
Guide progress: typed app-action events (applied, confirmed, rolled back) mark guide sections done through a declarative
doneWhenon each section. Phase 4 can use the same events for section highlighting.Other:
- Compose works on plain-HTTP origins (
safeRandomUUID()). - The dev server accepts
pop-osand*.ts.nethost names.
- Compose works on plain-HTTP origins (
Dependencies#
sonner@2.0.8@tanstack/db@1.0.0-rc.1.2, pinned to match@tanstack/react-db
Not in this PR#
- Guide code snippets still show placeholder code (Phase 3).
- The network panel still shows disabled controls (Phase 4).
Review#
Three rounds of independent Codex review, with every finding fixed and covered by tests:
- First review:
- out-of-order saves overwriting newer edits
- a failed refetch discarding an accepted write
- Compose failing on HTTP origins
- malformed JSON skipping the request log
- Re-check: debounced edits from two editor mounts still landed out of order, so all editors now share one paced mutation per note (
src/db/paced-per-record.ts). Multi-record transactions now confirm each record inside its write queue and keep accepted siblings. - Second re-check: pin, move and delete right after typing briefly reverted. All note changes now go through the note's one paced mutation, and permanent delete cancels unsent edits first.
TanStack DB behaviour this works around (intended, not a bug): each pending optimistic transaction overlays its whole-row snapshot, so an older pending change can briefly hide fields that a newer transaction on the same row already confirmed. This is documented in the mutations guide, covered by upstream tests, and was kept deliberately when TanStack/db#1701 proposed per-field merging. It's the same in 1.0.0-rc.1.3. The app follows from it:
- All changes to a note go through that note's one paced mutation.
- No transaction spans several records.
Dev-only URL switches (dev server only), used to reproduce timing bugs:
?latency=<ms>sets the fake API's latency.?failNext=1makes the next request fail.?saveDelay=<ms>sets the editor's save debounce.
A screw-bugs run with these switches caught the flicker on the pre-fix code and not on this branch.
vp check, vp test (123 tests) and vp build pass. Checked in a browser at 1440 and 390 px, in light and dark, including over the tailnet on plain HTTP.
🤖 Generated with Claude Code
- 1
fuko merged thisbcbe5dc
fuko closed this
Sign in to join this conversation.