fn-machine
Advanced tools
Comparing version 0.0.12 to 0.0.13
{ | ||
"name": "fn-machine", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "a tiny functional state machine", | ||
@@ -5,0 +5,0 @@ "main": "index", |
@@ -39,3 +39,2 @@ /** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
// throw if next state is undefined | ||
// throw new Error(`the transition '${event}' of current state '${current}', returned a non-existant desired state '${next.state}'.`); | ||
throw `the transition '${event}' of current state '${current}', returned a non-existant desired state '${next.state}'.`; | ||
@@ -54,3 +53,13 @@ } | ||
// enter _can_ change state | ||
newState.enter && newState.enter(next.context || context); | ||
// enter can also optionally return a new context | ||
const newContext = (newState.enter && newState.enter(next.context || context)); | ||
// since a common use case for an enter fn is to call an async method (usually loading data), | ||
// ignore the return if it's a promise. This is not foolproof but should | ||
// handle most cases. | ||
if (newContext && !(newContext instanceof Promise)) { | ||
context = next.context = newContext; | ||
// run changeCb again if context has changed | ||
changeCb(next); | ||
} | ||
} else { | ||
@@ -57,0 +66,0 @@ loggerFn(`state '${current}' does not handle event '${event}'.`); |
9978
188