You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.0 KiB
TypeScript

import { Machine, State, On, SideEffect, Goto, Interpreter, send, SideEffectFunction_T } from '../index';
const beginTimer : SideEffectFunction_T<C> = (ctx, e, self)=>{ setTimeout(()=>{ send(self, ['timer-finished',null]); }, 800); };
const log : SideEffectFunction_T<C> = (ctx, e, self)=>{ console.log(self.state); };
type S =
| 'green'
| 'yellow'
| 'red';
type E =
| ['entry',null]
| ['timer-finished',null];
type C = {};
const machine =
Machine(
State('green',
On('entry',
SideEffect(beginTimer),
SideEffect(log)
),
On('timer-finished',
Goto('yellow')
)
),
State('yellow',
On('entry',
SideEffect(beginTimer),
SideEffect(log)
),
On('timer-finished',
Goto('red')
)
),
State('red',
On('entry',
SideEffect(beginTimer),
SideEffect(log)
),
On('timer-finished',
Goto('green')
)
),
);
const actor = Interpreter<C>(machine, {context:{}});