@paybase/machine
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@paybase/machine", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "async/await finite state machine factory", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -54,3 +54,3 @@ # @paybase/machine | ||
#### `createMachineFactory({ stateKey = 'state', handlers = {}, transitions })` -> `MachineFactory` | ||
#### `createMachineFactory({ stateKey = 'state', allowCyclicalTransitions = false, handlers = {}, transitions })` -> `MachineFactory` | ||
@@ -60,4 +60,5 @@ The machine factory creator takes an options object containing 3 properties: | ||
- `stateKey` - defaults to `'state'`, determines the key on which the state will be defined on the context | ||
- `allowCyclicalTransitions` - defaults to `false`, determines whether the machine should allow cyclical transitions | ||
- `handlers` - defaults to `{}`, defines optional life-cycle hooks for the machine | ||
- `transitions` - required, defines transitions keyed by name containing `from`/`to` attributes of the type `string|array<string>` | ||
- `transitions` - required, defines transitions keyed by name containing `from`/`to` attributes of the type `string|number|array<string|number>` | ||
@@ -64,0 +65,0 @@ A machine factory is returned which is used to initialise a machine. |
@@ -64,3 +64,3 @@ const { | ||
const prepareThru = (finder, allEdges, stateKey) => { | ||
const prepareThru = (finder, allEdges, stateKey, allowCyclicalTransitions) => { | ||
const joins = getJoins(allEdges); | ||
@@ -80,4 +80,7 @@ const recurse = shortestPath(joins); | ||
const s = [ ctx[stateKey], ...states ]; | ||
if (first(s) === last(s) && s.length === 2) | ||
return false; | ||
if ( | ||
!allowCyclicalTransitions | ||
&& first(s) === last(s) | ||
&& s.length === 2 | ||
) return false; | ||
return getPaths(s); | ||
@@ -94,6 +97,6 @@ }; | ||
const prepareDefaultMethods = (edges, stateKey) => { | ||
const prepareDefaultMethods = (edges, stateKey, allowCyclicalTransitions) => { | ||
const finder = createFinder(edges); | ||
const allEdges = getAllEdges(edges); | ||
const createThru = prepareThru(finder, allEdges, stateKey); | ||
const createThru = prepareThru(finder, allEdges, stateKey, allowCyclicalTransitions); | ||
return (ctx) => { | ||
@@ -116,4 +119,7 @@ const thru = createThru(ctx); | ||
const s = [ ctx[stateKey], ...to ]; | ||
if (first(s) === last(s) && s.length === 2) | ||
throw new Error('Potential cyclic transition'); | ||
if ( | ||
!allowCyclicalTransitions | ||
&& first(s) === last(s) | ||
&& s.length === 2 | ||
) throw new Error('Potential cyclic transition'); | ||
@@ -133,3 +139,8 @@ const names = thru(...to); | ||
const createMachineFactory = ({ transitions, handlers = {}, stateKey = 'state' } = {}) => { | ||
const createMachineFactory = ({ | ||
transitions, | ||
handlers = {}, | ||
stateKey = 'state', | ||
allowCyclicalTransitions = false | ||
} = {}) => { | ||
if (!transitions) | ||
@@ -143,3 +154,3 @@ throw new Error('No transitions supplied'); | ||
const check = checkInitialState(states, stateKey); | ||
const createDefaultMethods = prepareDefaultMethods(edges, stateKey); | ||
const createDefaultMethods = prepareDefaultMethods(edges, stateKey, allowCyclicalTransitions); | ||
@@ -146,0 +157,0 @@ const init = (context = {}) => { |
26519
363
274