$ npm install @xstate/fsm
XState for Finite State Machines
This package contains a minimal, 1kb implementation of XState for finite state machines.
@xstate/fsm | XState | |
---|---|---|
Finite states | ✅ | ✅ |
Initial state | ✅ | ✅ |
Transitions (object) | ✅ | ✅ |
Transitions (string target) | ✅ | ✅ |
Delayed transitions | ❌ | ✅ |
Eventless transitions | ❌ | ✅ |
Nested states | ❌ | ✅ |
Parallel states | ❌ | ✅ |
History states | ❌ | ✅ |
Final states | ❌ | ✅ |
Context | ✅ | ✅ |
Entry actions | ✅ | ✅ |
Exit actions | ✅ | ✅ |
Transition actions | ✅ | ✅ |
Parameterized actions | ❌ | ✅ |
Transition guards | ✅ | ✅ |
Parameterized guards | ❌ | ✅ |
Spawned actors | ❌ | ✅ |
Invoked actors | ❌ | ✅ |
state.changed
If you want to use statechart features such as nested states, parallel states, history states, activities, invoked services, delayed transitions, transient transitions, etc. please use XState
.
npm i @xstate/fsm
import { createMachine } from '@xstate/fsm';
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } }
}
});
const { initialState } = toggleMachine;
const toggledState = toggleMachine.transition(initialState, { type: 'TOGGLE' });
toggledState.value;
const untoggledState = toggleMachine.transition(toggledState, {
type: 'TOGGLE'
});
untoggledState.value;
// => 'inactive'
import { createMachine, interpret } from '@xstate/fsm';
const toggleMachine = createMachine({});
const toggleService = interpret(toggleMachine).start();
toggleService.subscribe((state) => {
console.log(state.value);
});
toggleService.send({ type: 'TOGGLE' });
toggleService.send({ type: 'TOGGLE' });
toggleService.stop();
© 2010 - cnpmjs.org x YWFE | Home | YWFE