diff --git a/package.json b/package.json index 582a989..825f9f9 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "scripts": { "build": "esbuild --bundle --format=esm --minify --sourcemap --outdir=dist src/index.ts", - "build-tests": "esbuild --bundle --minify --outdir=dist/test test/*.ts" + "build-tests": "esbuild --bundle --minify --outdir=dist/tests tests/*.ts" }, "devDependencies": { "esbuild": "^0.17.18" diff --git a/src/index.ts b/src/index.ts index e69de29..97e079e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -0,0 +1,28 @@ +export interface Machine_T { + states: Array> +} +export interface State_T { + name: S; + ons: Array>; +} +export interface On_T { + eventName: E; + reactions: Array>; +}; +export interface Do_T { + type: 'Do'; + fn: DoFn_T; +}; +export type DoFn_T = (ctx,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, ...reactions:Array>) : 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} }; +export const Spawn = function(){}; +export const Unspawn = function(){}; \ No newline at end of file diff --git a/src/tests/00-basic.ts b/src/tests/00-basic.ts new file mode 100644 index 0000000..f22f633 --- /dev/null +++ b/src/tests/00-basic.ts @@ -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') + ) + ), + ); \ No newline at end of file