fn-machine
Advanced tools
Comparing version 0.0.13 to 0.0.14
{ | ||
"name": "fn-machine", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "a tiny functional state machine", | ||
@@ -33,3 +33,3 @@ "main": "index", | ||
"mocha": "^7.1.2", | ||
"np": "^6.2.3", | ||
"np": "^6.5.0", | ||
"sinon": "^7.3.2", | ||
@@ -36,0 +36,0 @@ "typescript": "^3.8.3" |
@@ -9,2 +9,3 @@ /** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
* @param {function(CurrentState)=} changeCb | ||
* @param {function(any)=} loggerFn | ||
* @return {function(string, Object=):CurrentState?} | ||
@@ -15,6 +16,6 @@ */ | ||
transitions: { | ||
[x: string]: (arg0: any, arg1: any) => { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
}; | ||
@@ -26,3 +27,3 @@ enter: Function; | ||
context?: any; | ||
}) => any): (arg0: string, arg1?: any) => { | ||
}) => any, loggerFn?: (arg0: any) => any): (arg0: string, arg1?: any) => { | ||
state: string; | ||
@@ -38,6 +39,6 @@ context?: any; | ||
transitions: { | ||
[x: string]: (arg0: any, arg1: any) => { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
}; | ||
@@ -44,0 +45,0 @@ enter: Function; |
@@ -19,3 +19,3 @@ /** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
return function send(event, detail) { | ||
return function send(event, detail = {}) { | ||
loggerFn(`sent '${event}'`); | ||
@@ -33,4 +33,12 @@ // if no event, return the current state | ||
const transition = active.transitions[transitionKey]; | ||
// if there's a transition, call it, otherwise just keep the current state. | ||
const next = transition ? transition(detail, context) : {state: current, context}; | ||
// if there's a transition function, call it | ||
const next = transition instanceof Function ? | ||
transition(detail, context) : | ||
// if the transition is a string, return it as the next state, and | ||
// automatically merge detail and context | ||
typeof transition === 'string' ? | ||
{state: transition, context: {...detail, ...context}} : | ||
// otherwise just keep the current state. | ||
{state: current, context}; | ||
// we only want to run exit, enter and the callback IF a transition was run. | ||
@@ -57,3 +65,3 @@ if (transition) { | ||
// 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 | ||
// ignore the return if it's a promise. This is not foolproof, but should | ||
// handle most cases. | ||
@@ -60,0 +68,0 @@ if (newContext && !(newContext instanceof Promise)) { |
@@ -5,3 +5,3 @@ /** | ||
/** | ||
* @typedef {function(Object, Object):CurrentState} Transition | ||
* @typedef {(string | function(Object, Object):CurrentState )} Transition | ||
*/ | ||
@@ -20,13 +20,13 @@ /** | ||
export default function state(name: string, transitions: { | ||
[x: string]: (arg0: any, arg1: any) => { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
}, enterFn?: (arg0: any) => any, exitFn?: (arg0: any) => any): { | ||
name: string; | ||
transitions: { | ||
[x: string]: (arg0: any, arg1: any) => { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
}; | ||
@@ -40,13 +40,13 @@ enter: Function; | ||
}; | ||
export type Transition = (arg0: any, arg1: any) => { | ||
export type Transition = string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
export type State = { | ||
name: string; | ||
transitions: { | ||
[x: string]: (arg0: any, arg1: any) => { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}); | ||
}; | ||
@@ -53,0 +53,0 @@ enter: Function; |
@@ -5,3 +5,3 @@ /** | ||
/** | ||
* @typedef {function(Object, Object):CurrentState} Transition | ||
* @typedef {(string | function(Object, Object):CurrentState )} Transition | ||
*/ | ||
@@ -8,0 +8,0 @@ /** |
10426
196