begin typescript types
parent
feeb933e90
commit
f6adaa8f53
@ -0,0 +1,28 @@
|
||||
export interface Machine_T<S,E> {
|
||||
states: Array<State_T<S,E>>
|
||||
}
|
||||
export interface State_T<S,E> {
|
||||
name: S;
|
||||
ons: Array<On_T<S,E>>;
|
||||
}
|
||||
export interface On_T<S,E> {
|
||||
eventName: E;
|
||||
reactions: Array<Do_T | Goto_T<S>>;
|
||||
};
|
||||
export interface Do_T {
|
||||
type: 'Do';
|
||||
fn: DoFn_T;
|
||||
};
|
||||
export type DoFn_T = (ctx,e,self)=>any;
|
||||
export interface Goto_T<S> {
|
||||
type: 'Goto';
|
||||
targetStateName: S;
|
||||
};
|
||||
|
||||
export const Machine = function<S,E>(...states:Array<State_T<S,E>>) : Machine_T<S,E> { return {states}; };
|
||||
export const State = function<S,E>(name:S, ...ons:Array<On_T<S,E>>) : State_T<S,E>{ return {name, ons}; };
|
||||
export const On = function<S,E>(eventName:E, ...reactions:Array<Do_T | Goto_T<S>>) : On_T<S,E>{ return {eventName, reactions}; };
|
||||
export const Do = function(fn:DoFn_T) : Do_T{ return {type:'Do', fn}; };
|
||||
export const Goto = function<S>(targetStateName:S) : Goto_T<S> { return {type:'Goto', targetStateName} };
|
||||
export const Spawn = function(){};
|
||||
export const Unspawn = function(){};
|
@ -0,0 +1,34 @@
|
||||
import { Machine, State, On, Do, Goto, Spawn, Unspawn } from '../index';
|
||||
|
||||
const beginTimer = ()=>{};
|
||||
|
||||
type S = 'green' | 'yellow' | 'red';
|
||||
type E = 'entry' | 'timer-finished';
|
||||
|
||||
const machine =
|
||||
Machine(
|
||||
State('green',
|
||||
On('entry',
|
||||
Do(beginTimer)
|
||||
),
|
||||
On('timer-finished',
|
||||
Goto('yellow')
|
||||
)
|
||||
),
|
||||
State('yellow',
|
||||
On('entry',
|
||||
Do(beginTimer)
|
||||
),
|
||||
On('timer-finished',
|
||||
Goto('red')
|
||||
)
|
||||
),
|
||||
State('red',
|
||||
On('entry',
|
||||
Do(beginTimer)
|
||||
),
|
||||
On('timer-finished',
|
||||
Goto('green')
|
||||
)
|
||||
),
|
||||
);
|
Loading…
Reference in New Issue