diff --git a/src/index.ts b/src/index.ts index d22695b..6936026 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,43 +1,43 @@ -type EventShape = [name:string, payload:any]; -export interface Machine_T { - states: Array> +type Event_T = [name:string, payload:any]; +export interface Machine_T { + states: Array> } -export interface State_T { +export interface State_T { name: S; - ons: Array>; + ons: Array>; } -export interface On_T { +export interface On_T { eventName: E[0]; - reactions: Array>; + reactions: Array | Goto_T>; }; -export interface Do_T { +export interface Do_T { type: 'Do'; - fn: DoFn_T; + fn: DoFn_T; }; -export type DoFn_T = (ctx,e,self)=>any; +export type DoFn_T = (ctx:C,e:E,self)=>any; export interface Goto_T { type: 'Goto'; targetStateName: S; }; -export const Machine = function(...states:Array>) : Machine_T { return {states}; }; -export const State = function(name:S, ...ons:Array>) : State_T{ return {name, ons}; }; -export const On = function(eventName:E[0], ...reactions:Array>) : On_T{ return {eventName, reactions}; }; -export const Do = function(fn:DoFn_T) : Do_T{ return {type:'Do', fn}; }; +export const Machine = function(...states:Array>) : Machine_T { return {states}; }; +export const State = function(name:S, ...ons:Array>) : State_T{ return {name, ons}; }; +export const On = function(eventName:E[0], ...reactions:Array | Goto_T>) : On_T{ return {eventName, reactions}; }; +export const Do = function(fn:DoFn_T) : Do_T{ return {type:'Do', fn}; }; export const Goto = function(targetStateName:S) : Goto_T { return {type:'Goto', targetStateName} }; -interface Tick_T { - doFunctions: Array +interface Tick_T { + doFunctions: Array> }; -const Tick = function(doFunctions : Array) : Tick_T{ return {doFunctions}; }; +const Tick = function(doFunctions : Array>) : Tick_T{ return {doFunctions}; }; -export interface Interpreter_T { - machine: Machine_T; +export interface Interpreter_T { + machine: Machine_T; state: S; context: C | {}; } -export function interpret(machine:Machine_T, options?:{state?:S, context?:C | {}}) : Interpreter_T{ +export function interpret(machine:Machine_T, options?:{state?:S, context?:C | {}}) : Interpreter_T{ let {state, context} = options || {}; if(typeof state === 'undefined'){ state = machine.states[0].name; } if(typeof context === 'undefined'){ context = {}; } @@ -46,12 +46,12 @@ export function interpret(machine:Machine_T, opti /** Helper function for `send()` */ -function getState(interpreter : Interpreter_T) : State_T{ - return interpreter.machine.states.find((state)=>state.name===interpreter.state) as unknown as State_T; +function getState(interpreter : Interpreter_T) : State_T{ + return interpreter.machine.states.find((state)=>state.name===interpreter.state) as unknown as State_T; } /** Helper function for `send()` */ -function getOns(state : State_T, event:E) : Array>{ +function getOns(state : State_T, event:E) : Array>{ return state.ons.filter((on)=>on.eventName===event[0]); } /** Inject an Event into the Interpreter's "tick queue". @@ -64,7 +64,7 @@ function getOns(state : State_T, event:E) : Array(interpreter : Interpreter_T, event:E){ +export function send(interpreter : Interpreter_T, event:E){ const state = getState(interpreter); const ons = getOns(state, event); const tick = Tick(ons @@ -75,10 +75,10 @@ export function send(interpreter : Interpreter_T{}; + return (ctx:C,e:E,self)=>{}; } else{ - return (ctx,e,self)=>{}; + return (ctx:C,e:E,self)=>{}; } }) ); diff --git a/src/tests/00-basic.ts b/src/tests/00-basic.ts index bf445e4..74ac08c 100644 --- a/src/tests/00-basic.ts +++ b/src/tests/00-basic.ts @@ -1,12 +1,13 @@ import { Machine, State, On, Do, Goto, Spawn, Unspawn } from '../index'; -const beginTimer = ()=>{}; +const beginTimer = (ctx:C, e:E)=>{}; type S = 'green' | 'yellow' | 'red'; type E = ['entry',null] | ['timer-finished',null]; +type C = null; const machine = - Machine( + Machine( State('green', On('entry', Do(beginTimer)