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.
41 lines
1001 B
TypeScript
41 lines
1001 B
TypeScript
import { Machine, State, On, Do, Goto, Spawn, Unspawn, interpret, Interpreter_T, send } from '../index';
|
|
|
|
const beginTimer = (ctx:C, e:E, self:Interpreter_T<S,E,C>)=>{ setTimeout(()=>{ send(self, ['timer-finished',null]); }, 800); };
|
|
const log = (ctx:C, e:E, self:Interpreter_T<S,E,C>)=>{ console.log(self.state); };
|
|
|
|
type S = 'green' | 'yellow' | 'red';
|
|
type E = ['entry',null] | ['timer-finished',null];
|
|
type C = {};
|
|
|
|
const machine =
|
|
Machine<S,E,C>(
|
|
State('green',
|
|
On<S,E,C>('entry',
|
|
Do(beginTimer),
|
|
Do(log)
|
|
),
|
|
On<S,E,C>('timer-finished',
|
|
Goto('yellow')
|
|
)
|
|
),
|
|
State('yellow',
|
|
On<S,E,C>('entry',
|
|
Do(beginTimer),
|
|
Do(log)
|
|
),
|
|
On<S,E,C>('timer-finished',
|
|
Goto('red')
|
|
)
|
|
),
|
|
State('red',
|
|
On<S,E,C>('entry',
|
|
Do(beginTimer),
|
|
Do(log)
|
|
),
|
|
On<S,E,C>('timer-finished',
|
|
Goto('green')
|
|
)
|
|
),
|
|
);
|
|
|
|
const actor = interpret(machine, {context:{}}); |