# createMachine
The createMachine
function creates a state machine. It takes an object of states with the key being the state name. The value is usually state but might also be invoke.
Table of Contents
This is a common example that provides 2 states and a context
import { createMachine, state } from 'robot3';
const context = () => ({
first: 'Wilbur',
last: 'Phillips'
});
const machine = createMachine({
idle: state(),
input: state()
}, context);
# [initial]
Optionally can provide the initial state as the first argument. If no initial state is provided then the first state listed will be the initial state.
const toggleMachine = initial => createMachine(initial, {
active: state(
transition('toggle', 'inactive')
),
inactive: state(
transition('toggle', 'active')
)
});
const myMachine = toggleMachine('inactive');
const service = interpret(myMachine, () => {});
console.log(service.machine.current, 'inactive');
# states
An object of states, where each key is a state name, and the values are one of state or invoke.
# context
context(initialContext)
A second argument to createMachine
is the context
for the machine; a function that returns an object of extended state values. This is useful for storing values coming from places like forms.
The context
function receives an initialContext
argument. This is anything passed as the third argument to interpret like so:
const context = initialContext => ({
foo: initialContext.foo
});
const machine = createMachine({
idle: state()
}, context);
const initialContext = {
foo: 'bar'
};
interpret(machine, service => {
// Do stuff when the service changes.
}, initialContext);