I Wired the Notifier Before the Model: A Post-Mortem on "Notification Before State"

I want to own a design mistake that shipped in the web interface I work on. Its symptom confuses people and wears down their trust.

You get a notification saying “machine X went offline”, and the list on screen still shows that machine as online. Or a toast says an operation finished, and the row it acted on still shows the old state. A few seconds later the screen catches up. In those seconds the interface is contradicting itself, and the user is left wondering which part to believe.

Nothing here is a typo or an off-by-one. I made an architecture decision, and it produces this symptom by design. Below I explain what I built, why it causes the problem, and what I’m changing.

What I built

I never built an application model. I used the server-state query cache (TanStack Query) as the model. Every screen reads data through shared hooks, and each read is keyed by its URL path and parameters. That felt clean. In practice it means:

  • One entity can live in many places. The same session or machine sits in a list entry, a detail entry, and every parameter variant of every route that mentions it. Each copy polls on its own, and nothing ties them together.
  • Freshness comes from polling. By default every mounted read refetches every 5 seconds, and some reads poll more slowly. When the tab is hidden, nothing polls at all.

I wired the event stream to the notifier, not to the state. The backend pushes events such as “machine changed state”, “alert delivered” and “pull request merged”. The handler I wrote for that stream raises the notification and that’s it. Depending on the event type:

  • Some events trigger a refetch that the handler doesn’t wait for, and the notification goes out in the same tick.
  • Some events don’t touch the client’s state at all. A machine state event carries the machine, its old state and its new state, which is everything needed to update the row directly, and I ignored all of it. The screen changed only on the next poll.
  • Alerts had three separate readers for one fact. The system notification came from the stream, the in-app toast and badge came from comparing one poll with the previous one, and the alerts screen read a different variant of the same route on a different interval. Each reader had a different delay, and none of them was the source of truth.

I fired toasts on completion, not on state change. The operation-finished toast fired when the operation record said “done”. But the operator is looking at the resource the operation changed, and that update arrived through a refetch issued after the toast. React runs child effects before parent effects, so the toast was always raised before the refetch had even been requested.

Startup had the same flaw. The splash screen lifted after the first rendered frame plus a short minimum delay, whether or not any data had arrived. The first screen was a field of spinners from a dozen independent requests, loading in whatever order components happened to mount.

Why it causes the symptom

The root cause fits in one sentence: notifications and screens were fed by different sources, and the notification’s source was faster.

A pushed event arrives in milliseconds. The screen updates on a poll interval or after a round trip. That gap is the symptom. And because there was no single model, an event saying “machine X is offline” had no single place to write that fact. The only safe general response was “invalidate everything and refetch”, and that always costs at least one round trip.

The parts that worked confirm the diagnosis:

  • Session events: the client writes them straight into the cache and shows no toast. That screen never had the problem.
  • Actions that patch the cached data with the server’s answer before toasting: those controls never had the problem either.
  • The pull requests page: it reads from a real store (zustand) that’s kept in sync with the cache. It lagged only because the notification came before the refetch instead of after it.

What I should have built

Model–view–controller, applied seriously: one model that views only read, with every input writing to the model and never to a view. By “input” I mean a fetch, a user action or a pushed event.

A real model, built at startup

  • Hold application state in a state management library (zustand or similar). A query cache used as a model doesn’t count, and neither do module-level variables with a hand-rolled listener set.
  • Fetch the initial state at startup and keep the splash screen up until the critical data has arrived. The splash should lift when data arrives, not after a frame or a timer.
  • Decide what happens when the app starts offline. If offline is a rare fallback, keep the splash until the connection attempt times out. If offline is a normal mode, offer “start offline” after a short wait. Spinning forever is not a decision.

Declared loading tiers

  • Critical: the splash waits for it.
  • Likely: used in most sessions, fetched in the background right after startup.
  • Lazy: fetched when the user navigates to the section that needs it, with spinners only where that data will appear. The per-widget loading region we already have does this well, and it stays.

Before, these tiers existed only by accident, depending on what the frame happened to mount. They need to be explicit.

Views only read

  • A page may trigger a fetch, but the result goes into the model. The page re-renders because the model changed.
  • No component keeps its own private copy of state that disagrees with the model. A few did, for example hiding a question’s answer choices locally after submitting while the stored data still showed the question pending. Those become model writes.
  • Each entity lives in one place. List and detail views read from one normalised entry, so one write updates every screen.

Events update the model first, then notify

This is the rule that would have prevented the whole thing:

Every backend event is applied to the model first. The notification is a side effect of that same path, raised only after the state has changed.

Events that exist only to notify still go through the model and simply make no change. That guarantees the UI never announces something the page doesn’t show yet.

In concrete terms:

  • When a machine changes state, update that machine in the model from the event’s own data, then notify.
  • For events that don’t carry enough data to apply directly, wait for the re-read to finish, then notify.
  • Raise the operation-finished toast after the affected resource is up to date, not when the operation record says it’s done.
  • Give alerts one source in the model that the stream updates, and have the badge, toast and screen all read from it.
  • Make backend event payloads rich enough to apply directly. An event that is “only an announcement” forces a refetch, and the refetch is where the window opens.

What I’m doing differently

  1. Start with “where does this fact live?” Before I wire up any notification, I’ll find the single place in the model that the event updates. If there isn’t one, that’s the bug, not a detail for later.
  2. Order side effects on purpose. Write the state, then announce it. I won’t rely on effect timing or “the poll will catch it.”
  3. Treat polling as a backup. Polling is for recovering from missed events. If a screen depends on it to stay current, the design is missing a push path.
  4. Fix the spec too. Our design documents are authoritative over the code, and the current spec says the splash lifts on the first frame. That rule gets changed alongside the code, plus new rules for the critical data set, the loading tiers, the offline decision, the single model and state-before-notification.
  5. Review new features for the gap. For any feature that both shows data and notifies about it, the review question becomes: can the notification ever arrive before the screen changes? If yes, it isn’t done.

The most frustrating part is that the right pattern was already in the codebase in a couple of places, and those places worked. I didn’t make it the rule. Now it is: state first, notification second, every time.

Advanced search: filter by model or tag