import S from '@oversword/super-small-state-machine'
const fibonacciGenerator = new S({
input: 1,
result: 0,
lastResult: 0,
}, {
initial: 'testStart',
testStart: [
{
if: ({ result }) => result === 0,
then: () => ({ result: 1 })
},
'fibonacci'
],
fibonacci: [
({ result, lastResult }) => ({
lastResult: result,
result: result + lastResult
}),
'testEnd',
],
testEnd: [
({ input }) => ({ input: input-1 }),
{
if: ({ input }) => input > 1,
then: 'fibonacci',
else: S.return,
}
]
}).input(number => ({ input: number }))
const twelfthFibonacci = fibonacciGenerator(12)
console.log(twelfthFibonacci) // prints 144
const machine = new S(defaultContext, executionProcess)
const result = machine(initialContext[, runConfig])
const machine = new S(defaultContext, executionProcess)
const modifiedMachine = machine.input(trandformInput).output(transformOutput)
const result = modifiedMachine(initialContext)
const machine = new S(defaultContext, executionProcess)
const secondContext = machine.step(firstContext)
const thirdContext = machine.step(secondContext)
const machine = new S(defaultContext, (context) => {
return {
update: 'value'
}
})
const machine = new S(defaultContext, [
firstAction,
secondAction,
thirdAction,
])
const machine = new S(defaultContext, {
if: ({ someProperty }) => someProperty === 'value',
then: successAction,
else: failAction,
})
const machine = new S(defaultContext, {
switch: ({ someProperty }) => someProperty,
case: {
firstValue: handleFirstValue,
secondValue: handleSecondValue,
default: fallbackHandler,
}
})
const machine = new S(defaultContext, {
initial: 'firstStage',
firstStage: [
firstAction,
'secondStage',
],
secondStage: [
secondAction
]
})
const machine = new S(defaultContext, [
firstAction,
secondAction,
thirdAction,
{
if: endCondition,
then: S.return,
else: 0
}
])
const machine = new S(defaultContext, {
firstStage: [
firstAction,
'secondStage',
],
secondStage: [
secondAction,
'continueOrExit',
],
continueOrExit: {
if: endCondition,
then: S.return,
else: 'firstStage'
}
})
const machine = new S(defaultContext, {
firstStage: [
firstAction,
'secondStage',
],
secondStage: [
secondAction,
'continueOrExit',
],
continueOrExit: {
if: endCondition,
then: S.return,
else: () => ['firstStage',0]
}
})
const childMachine = new S(childContext, childSequence)
const parentMachine = new S(parentContext, [
firstAction,
childMachine
.input(({ parentProperty }) => ({ childProperty: parentProperty }))
.output((resultValue) => ({ result: resultValue })),
])