Finite State Machines for UI vs Signals and Fine-Grained Reactivity (Reactive Graph)
Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.
Structured Comparison Matrix
| Dimension | Finite State Machines for UI | Signals and Fine-Grained Reactivity (Reactive Graph) |
|---|---|---|
| Mental Model | Replaces overlapping boolean flags (isLoading, isError, isRetrying) with a discriminated union and explicit (state, event) transition table that makes invalid states unrepresentable. | Reactive model built on a directed acyclic dependency graph (Signal, Computed, Effect) updating only subscribed DOM nodes without virtual tree diffing. |
| Maturity & Domain | Baseline · Widely · State & Architecture Chrome 1+ (Pure JS / Switch-Case) · Firefox 1+ (Pure JS / Switch-Case) · Safari 1+ (Pure JS / Switch-Case) | Experimental · State & Architecture Chrome TC39 Stage 1 (polyfill) · Firefox TC39 Stage 1 (polyfill) · Safari TC39 Stage 1 (polyfill) |
| Prerequisites | Foundational primitive | |
| When Not to Use | When not to use an FSM: For continuous numeric values (scroll offset, canvas coordinates) or simple independent form fields, plain state or reactive signals are far lighter. | When not to use Signals: For simple forms or mostly static pages, native DOM primitives avoid unnecessary abstraction overhead; for multi-step workflows with illegal state combinations, pair or replace raw signals with Finite State Machines. |
Engine Mechanics & Trade-Offs
Replaces N overlapping boolean flags (2^N states) with a discriminated union of valid states and an explicit (state, event) -> nextState transition table.
Key Pitfalls: Event explosion on simple toggles: Writing a 60-line statechart configuration for an open/closed disclosure panel adds ceremony with zero safety gain. Forgetting cleanup on state exit: Cancelling an in-flight AbortController when leaving loading -> idle must happen deterministically on transition, or stale responses st
Glitch-free push-pull propagation over an acyclic observer graph.
Key Pitfalls: Effects writing to tracked signals (infinite loop): An effect mutating a signal it reads re-triggers the graph indefinitely until stack overflow. Losing reactivity via destructuring: Extracting const v = signal.get() outside the tracked execution scope breaks dynamic subscription.