Atomic States
Atomic states are used as leaves of a state tree. They do not have children.
Usage
Use atomic(...)
to set up state configuration and resolveState(...)
to initialize it.
import { atomic, emitEvent, resolveState } from 'hine';let count = 0;const counterConfig = atomic('counter', { on: { increment: () => (count += 1), },});const counterState = resolveState(counterConfig);emitEvent(counterState, 'increment');
In this example, counterState
is set up to increment count
by 1
each time an 'increment'
event is emitted.
Active Children
Active states are those that can receive and handle emitted events at a particular time. Atomic states themselves can handle events, but since they have no children, they have zero active child states.
They stand in contrast to compound states which have exactly one active child, and parallel states , where all children are simulataneously active.
// Always `true` for atomic statesconsole.log(counterState.activeChildren.length === 0);
Children
Atomic states have no children.
// Always `true` for atomic statesconsole.log(counterState.children.size === 0);
Hooks
Atomic states accept hooks such as afterEntry
and beforeExit
. Hooks respond to life-cycle events of a state.
import { atomic, emitEvent, resolveState } from 'hine';let count;const initializeCount = () => (count = 0);const counterConfig = atomic('counter', { hooks: { afterEntry: initializeCount, }, on: { increment: () => (count += 1), },});const counterState = resolveState(counterConfig);emitEvent(counterState, 'increment');
When counterState
is resolved, it will enter the 'counter'
state and initialize count
to 0
.