Workbench / The TiffinBox Build
S21 · V2·21
A Display With an Architecture
Volume 2, chapter 21. The kitchen display had grown to 200 lines of flags, handlers and DOM code. Stage 21 splits it into a pure core (a connection state machine and an open-orders model, tested in Node with 20,000 random event sequences), adapters (stream, commands, wake lock) and a keyed view; moves formatting into one Intl-based module shared with search; and recovers the load time stage 20 gave up by rendering the first screenful on the server and the rest one chunk per frame. On the way: Java's own formatter writes ₹123,456.00 for Indian English, a page route was swallowing the new module files with a 405, and the first `Intl.NumberFormat` cost 56 ms in the middle of a rush. A fitness function now checks the architecture on every build.
Stage 21: a display with an architecture
The brief
What you'll build.
web-src/kitchen/connection.ts: the connection as a state machine (connecting,live,reconnecting,backingOff), pure;describe()maps each state to one status line;web-src/kitchen/orders.ts: open orders as data (applyOrder,applySnapshot,lateness), pure;web-src/announcer.tsandweb-src/format.ts, shared: batched announcements; money and counted nouns throughIntl(plural rules per locale, Indian digit grouping), formatters created once and warmed while idle;web-src/kitchen.ts: wiring and view only: the EventSource adapter sends events to the machine and acts on the state it returns; cards are keyed; large snapshots render one chunk of 50 per frame;- server: the display page renders the first 40 cards (the rest say "N more open orders: the live view (JavaScript) shows them all");
rupees()in Java groups digits the Indian way; the/kitchen/k<n>page route is exact; - checks:
web-tests/kitchen.test.mjs(6 tests) andweb-tests/boundaries.mjs(architecture rules) incheck-js.sh; aPagesTestthat Java and the browser format the same amounts the same way.
Why a real company needs this. Code that works and keeps growing becomes code nobody can change safely. The display is about to be rebuilt in React (stage 22) and Angular (stage 23); if its logic lives in DOM handlers, it has to be rewritten each time. A pure core, with adapters and a view around it, can be tested without a browser and moved between frameworks. And formatting that differs between server and browser is the kind of bug customers notice first: prices.
Why now. Chapter 21 is UI architecture: UI as a function of state, boundaries, composition, design systems, dependency direction and reactivity. The display is the build's most complex screen, and stages 17–20 each added to it without restructuring.
What you'll have at the end.
- a display whose decisions are pure and tested, and whose effects are at the edges;
- load time at 500 orders back to no long task over 70 ms, without hiding anything from screen readers;
- ADR-0023: the frontend's module structure and its rules.
Theory to own
- Pure core, imperative shell. What goes in each, and what does it buy you for testing and for moving to a framework?
- State machines. Why model the connection as one state with transitions rather than several flags? (Companion, Gaps 1.)
- Dependency direction. Which way may
search,kitchen,format,announcerandlibimport each other, and how do you keep it that way? - Formatting. Why one formatting module, and why must the server format the same way? (Companion, Gaps 2.)
- Rendering large lists. How do you show 500 cards without a long task and without hiding content from assistive technology? (Stage 20's trade-off.)
Build it
Step 1: the connection as a state machine
Write kitchen/connection.ts: a Conn union (connecting with an attempt count and an optional outage start, live, reconnecting, backingOff with a reason), events (message, error with closed, silence, retryTimer), next(conn, event, now) with a switch per state, backoffMs(attempt) (full jitter, capped at 30 s), and describe(conn, clock). In kitchen.ts, send(event) computes the next state, renders the status, and performs effects on transitions: entering backingOff closes the stream and starts the timer; leaving it for connecting opens a new stream. Write tests: 20,000 random sequences of 15 events stay in designed states, every state is reachable, and the status line always agrees with the state.
Checkpoint. The tests pass; the stage 17 browser scenarios (restart, 503s, silence) still pass.
Step 2: orders as data, formatting in one place
Write kitchen/orders.ts: applyOrder(open, order) returns added, updated, removed or ignored; applySnapshot(open, orders) returns the ids that disappeared; lateness(status, at, now) returns minutes and whether that's late. Write format.ts with money(paise) (Intl.NumberFormat currency, en-IN by default) and count(n, forms) (Intl.PluralRules), and use them in search and the announcer. Then make the server's rupees() match: add a PagesTest with seven amounts and the browser's output for them.
Checkpoint. Java and the browser agree on every amount.
Step 3: first screen on the server, the rest in chunks
On the server, render only the first 40 open orders (FIRST_SCREEN), with a #more-tickets note for the rest. In kitchen.ts, renderAll() renders a snapshot in chunks of 50 cards: each chunk in its own task, and the next one only after the browser has produced a frame (requestAnimationFrame then setTimeout), with a generation number so a newer snapshot cancels an older rendering. Add a tb:chunk measure and re-run the stage 18 profile.
Checkpoint. At 500 orders, 4× throttled: no long task from rendering.
Step 4: the first announcement was a long task
After step 3, the profile still showed one long task during updates, attributed by Long Animation Frames (with source function names now printed) to flush in announcer.js: about 70 ms. The write to the live region measured under 1 ms (tb:announce). Measure what else flush does.
Checkpoint. No long tasks during updates.
Step 5: a fitness function for the architecture
Write web-tests/boundaries.mjs: read every module in web-src, and fail if a pure module (kitchen/connection.ts, kitchen/orders.ts) mentions document, window, timers, EventSource or fetch; if a feature imports another feature; if shared code imports a feature; or if anything assigns innerHTML/outerHTML or calls insertAdjacentHTML. Add it to check-js.sh, and prove it can fail.
Checkpoint. It passes on the code, and fails on three planted violations.
Senior and architect lens
1. Architecture is where code goes when it changes. The display was refactored now because the next two stages rebuild its view in two frameworks. The pure core and adapters mean stage 22 rewrites kitchen.ts's view (about 80 lines) and nothing else.
2. ADR-0023: frontend module structure. Write it with these points:
- Decision: features (
search,kitchen) depend on shared modules (lib,api,contract,format,announcer); a feature's decisions live in pure modules under its folder; effects and DOM live in its entry module. - Rules (checked by
boundaries.mjs): no DOM, timers or network in pure modules; no feature-to-feature imports; no shared-to-feature imports; no HTML from strings. - Formatting: all user-visible numbers, money, dates and counts go through
format.ts(browser) and the matching server helpers, tested against the same values. - Rendering: the server renders the first screenful; scripts render the rest in chunks of one frame each; nothing is hidden from the accessibility tree.
- Revisit when: a framework arrives (stage 22: the rules extend to components), or a second team works in the frontend (stage 30: package boundaries replace folder boundaries).
3. Make the illegal states impossible, then test the legal ones. The machine removes a class of bugs by construction; the random-sequence test checks what's left, 20,000 times in under a second. The same pairing as stage 5's lifecycle table and its exhaustive history test.
4. Two implementations need one oracle. Java and the browser both format money. The test takes its expected values from the browser, so the two implementations are checked against each other, not against someone's memory of what Indian grouping looks like.
5. Profiles find what reasoning doesn't. Nobody would guess that the first announcement costs 56 ms because of locale data, or that chunks without a frame between them just move the long task into layout. Each was found by the named measures and Long Animation Frames attribution from stage 18, with function names added in this stage.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| the view is hand-written DOM code | every new screen repeats keyed-update and focus logic | stage 22 (React components over the same core) |
| a screen-reader user wants one station only | one list, one announcer for the kitchen | stage 25 (filters and views as state) |
locale is fixed to en-IN |
no language choice for staff | Volume 3 (staff profiles with a language preference; stage 25 stores the theme preference the same way) |
| architecture rules are regular expressions | they can't see type-level dependencies or packages | stage 30 (package boundaries and a real dependency checker) |
| no visual tests | layout changes are noticed by eye | stage 26 (component-level screenshots) |
Evidence
check-js.shpasses: contract, types, architecture rules, 9 modules, 21 tests; the planted-violation run is in the README.- 28 server tests pass, including the formatting test with the browser's values; the Java
NumberFormatoutput is in the README. - The stage 18 profile with each step of this stage (
setTimeoutchunks, frame-paced chunks, warmedIntl) is recorded. - Stages 13–20 still pass (
stage20.pycounts every order heading in the browser's tree). - ADR-0023 is committed.
Carry forward
kitchen/connection.tsandkitchen/orders.tsare reused unchanged by the React display (stage 22) and the Angular one (stage 23).format.tsis the only way numbers reach the screen; Volume 3's staff profiles make its locale a preference.boundaries.mjsgrows with each stage; stage 30 replaces it with package-level rules.- First screen on the server, the rest in chunks is the rendering rule stage 24 (server rendering and hydration) builds on.