sync send works

This commit is contained in:
Brian Sakal
2023-05-12 00:41:47 -04:00
parent 0495f1d87a
commit e328533629
6 changed files with 77 additions and 45 deletions
+19 -13
View File
@@ -1,35 +1,41 @@
import { Machine, State, On, Do, Goto, Spawn, Unspawn } from '../index';
import { Machine, State, On, Do, Goto, Spawn, Unspawn, interpret, Interpreter_T, send } from '../index';
const beginTimer = (ctx:C, e:E)=>{};
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 = null;
type C = {};
const machine =
Machine<S,E,C>(
State('green',
On('entry',
Do(beginTimer)
On<S,E,C>('entry',
Do(beginTimer),
Do(log)
),
On('timer-finished',
On<S,E,C>('timer-finished',
Goto('yellow')
)
),
State('yellow',
On('entry',
Do(beginTimer)
On<S,E,C>('entry',
Do(beginTimer),
Do(log)
),
On('timer-finished',
On<S,E,C>('timer-finished',
Goto('red')
)
),
State('red',
On('entry',
Do(beginTimer)
On<S,E,C>('entry',
Do(beginTimer),
Do(log)
),
On('timer-finished',
On<S,E,C>('timer-finished',
Goto('green')
)
),
);
);
const actor = interpret(machine, {context:{}});