Connectivity Monitor
This example monitors the health of a network connection with a three-state FSM. It simulates health checks client-side, without a backend, and drives a real-time UI entirely through FSM event subscriptions. The browser’s online/offline events feed the FSM as inputs, and everything downstream is reactive.
What it demonstrates
Section titled “What it demonstrates”- Async operations in
_onEnter: Thecheckingstate starts an async health check and feeds the result back as an FSM input viafsm.handle(). The FSM stays synchronous, and the async work happens in the side effect. AbortControllercleanup in_onExit: Each in-flight health check gets anAbortController. When the FSM leaveschecking,_onExitcallsabort()so stale callbacks cannot trigger unexpected transitions.- Timer management co-located in
_onEnter/_onExit: The retry timer (offline) and heartbeat timer (online) are both started in_onEnterand cleared in_onExit. Cleanup is guaranteed regardless of which input caused the transition. - Custom events emitted from handlers:
checking._onEnteremitscheckCountUpdatedso the UI can display progress without polling state.offline._onEnteremitsmaxChecksReachedwhen the retry cap is hit. - Event-driven UI:
main.tssubscribes totransitionedand custom events. The UI readscurrentState()once for the initial render and reacts to events after that.
State diagram
Section titled “State diagram”| From | Input | To |
|---|---|---|
| online | connectionLost | checking |
| offline | connectionRestored | checking |
| offline | retryCheck | checking |
| checking | healthCheckPassed | online |
| checking | healthCheckFailed | offline |
| checking | connectionLost | offline |
The online state also runs a periodic heartbeat. If it fails, it fires connectionLost, the same input the browser offline event sends, and the FSM does not distinguish the source.
Key pattern
Section titled “Key pattern”The checking state is the core of the example: _onEnter starts async work and feeds the result back as an FSM input. _onExit aborts any in-flight request so stale callbacks can’t fire after the FSM leaves the state.
fsm is declared before createFsm() so _onEnter callbacks can close over it. _onEnter itself runs synchronously during createFsm(), and the closure is safe because only the promise continuations created inside _onEnter read fsm. Those continuations run after createFsm() returns and fsm is assigned.