fn-machine
Advanced tools
Comparing version 0.0.9 to 0.0.10
{ | ||
"name": "fn-machine", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "a tiny functional state machine", | ||
@@ -5,0 +5,0 @@ "main": "index", |
@@ -29,15 +29,19 @@ /** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
const transition = active.transitions[transitionKey]; | ||
// if there's a transition, call it, otherwise just return the current state. | ||
// if there's a transition, call it, otherwise just keep the current state. | ||
const next = transition ? transition(detail, context) : {state: current, context}; | ||
// if the current state has an exit function, run it. | ||
active.exit && active.exit(); | ||
const newState = states.find(s => s.name === next.state); | ||
// if the new state has an enter function, run it as well. | ||
newState.enter && newState.enter(next.context || context); | ||
// update current | ||
current = next.state; | ||
// update next.context if necessary | ||
next.context = context = next.context ? next.context : context; | ||
// call callback with the latest state. | ||
changeCb(next); | ||
// we only want to run exit, enter and the callback IF a transition was run. | ||
if (transition) { | ||
// if the current state has an exit function, run it. | ||
active.exit && active.exit(); | ||
const newState = states.find(s => s.name === next.state); | ||
// if the new state has an enter function, run it as well. | ||
newState.enter && newState.enter(next.context || context); | ||
// update current | ||
current = next.state; | ||
// update next.context if necessary | ||
next.context = context = next.context ? next.context : context; | ||
// call callback with the latest state. | ||
changeCb(next); | ||
} | ||
return next; | ||
@@ -44,0 +48,0 @@ } |
6497
167