Skip to content

Watch

watch runs actions whenever a context (or computed) field changes — in any state, for the machine’s whole lifetime. It’s the declarative form of “subscribe to a field and react.”

import { machine } from '@dunky-dev/state-machine'
const m = machine({
initial: 'idle',
context: { query: '' },
watch: {
query: ['runSearch'], // whenever `query` changes → run runSearch
},
states: { idle: {} },
implementations: {
actions: {
runSearch: ({ context, send }) => send({ type: 'search', q: context.query }),
},
},
})

Any time query changes — from any transition, in any state — runSearch fires. No need to add the action to every transition that touches query.

Watch actions obey the same run-to-completion rule as events: they’re queued and run after the transition that changed the field fully settles. A watcher writing context can’t re-enter a transition in flight.

watchcomputed
Use forside-effects (send, log, sync)derived values (filter, count, sum)
Runs whenfield changesread, if input changed
Returnsnothinga value

Use watch when you want to do something in response to a field changing. Use computed when you want to derive a value from fields.

watch: {
query: ['runSearch'],
activeIndex: ['scrollIntoView', 'announceSelection'],
}