fn-machine
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "fn-machine", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "a tiny functional state machine", | ||
@@ -5,0 +5,0 @@ "main": "index", |
/** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
/** @typedef {import('./fn-state').State} State */ | ||
/** | ||
* @description send an event through the machine | ||
* @callback sendFn | ||
* @param {string=} event | ||
* @param {any=} detail | ||
* @return {CurrentState} | ||
*/ | ||
/** | ||
* @description define a state machine | ||
@@ -9,4 +16,4 @@ * @param {Array<!State>} states | ||
* @param {function(CurrentState)=} changeCb | ||
* @param {function(any)=} loggerFn | ||
* @return {function(string, Object=):CurrentState?} | ||
* @param {function(...any)=} loggerFn | ||
* @return {sendFn} | ||
*/ | ||
@@ -16,3 +23,3 @@ export default function machine(states: { | ||
transitions: { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
[x: string]: string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -27,6 +34,3 @@ context?: any; | ||
context?: any; | ||
}) => any, loggerFn?: (arg0: any) => any): (arg0: string, arg1?: any) => { | ||
state: string; | ||
context?: any; | ||
}; | ||
}) => any, loggerFn?: (...arg0: any[]) => any): sendFn; | ||
export type CurrentState = { | ||
@@ -39,3 +43,3 @@ state: string; | ||
transitions: { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
[x: string]: string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -48,1 +52,5 @@ context?: any; | ||
}; | ||
export type sendFn = (event?: string, detail?: any) => { | ||
state: string; | ||
context?: any; | ||
}; |
/** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
/** @typedef {import('./fn-state').State} State */ | ||
/** | ||
* @description send an event through the machine | ||
* @callback sendFn | ||
* @param {string=} event | ||
* @param {any=} detail | ||
* @return {CurrentState} | ||
*/ | ||
/** | ||
@@ -10,4 +18,4 @@ * @description define a state machine | ||
* @param {function(CurrentState)=} changeCb | ||
* @param {function(any)=} loggerFn | ||
* @return {function(string, Object=):CurrentState?} | ||
* @param {function(...any)=} loggerFn | ||
* @return {sendFn} | ||
*/ | ||
@@ -21,7 +29,9 @@ export default function machine(states, initialState, initialContext, changeCb = function(){}, loggerFn = function(){}) { | ||
first && first.enter && first.enter(); | ||
return function send(event, detail = {}) { | ||
loggerFn(`sent '${event}'`); | ||
loggerFn(`sent '${event}'`, detail? detail: ''); | ||
// if no event, return the current state | ||
if (!event) { | ||
loggerFn(`no event. returning currentState`); | ||
loggerFn(`no event. returning current state '${current}'`); | ||
loggerFn('context:', context); | ||
return {state:current, context}; | ||
@@ -60,2 +70,3 @@ } | ||
loggerFn(`state changed to '${next.state}'`); | ||
loggerFn('context is now:', next.context); | ||
changeCb(next); | ||
@@ -62,0 +73,0 @@ // if the new state has an enter function, run it as well. |
@@ -5,3 +5,3 @@ /** | ||
/** | ||
* @typedef {(string | function(Object, Object):CurrentState )} Transition | ||
* @typedef {(string | ((detail:any, context:any) => CurrentState ))} Transition | ||
*/ | ||
@@ -20,3 +20,3 @@ /** | ||
export default function state(name: string, transitions: { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
[x: string]: string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -28,3 +28,3 @@ context?: any; | ||
transitions: { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
[x: string]: string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -41,3 +41,3 @@ context?: any; | ||
}; | ||
export type Transition = string | ((arg0: any, arg1: any) => { | ||
export type Transition = string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -49,3 +49,3 @@ context?: any; | ||
transitions: { | ||
[x: string]: string | ((arg0: any, arg1: any) => { | ||
[x: string]: string | ((detail: any, context: any) => { | ||
state: string; | ||
@@ -52,0 +52,0 @@ context?: any; |
/** | ||
* @typedef {{state:string, context?:any}} CurrentState | ||
*/ | ||
/** | ||
* @typedef {(string | function(Object, Object):CurrentState )} Transition | ||
* @typedef {(string | ((detail:any, context:any) => CurrentState ))} Transition | ||
*/ | ||
@@ -7,0 +8,0 @@ /** |
15369
297