remove typescript generics

they were getting in the way; it can be developed after the lib is
more mature
This commit is contained in:
Brian Sakal
2023-05-14 11:50:30 -04:00
parent e328533629
commit 26383090ba
6 changed files with 401 additions and 48 deletions
+17 -12
View File
@@ -1,38 +1,43 @@
import { Machine, State, On, Do, Goto, Spawn, Unspawn, interpret, Interpreter_T, send } from '../index';
import { Machine, State, On, Do, Goto, Spawn, Unspawn, interpret, Interpreter_T, send, Event_T } 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); };
const beginTimer = (ctx:C, e:Event_T, self:Interpreter_T)=>{ setTimeout(()=>{ send(self, ['timer-finished',null]); }, 800); };
const log = (ctx:C, e:Event_T, self:Interpreter_T)=>{ console.log(self.state); };
type S = 'green' | 'yellow' | 'red';
type E = ['entry',null] | ['timer-finished',null];
type S =
| 'green'
| 'yellow'
| 'red';
type E =
| ['entry',null]
| ['timer-finished',null];
type C = {};
const machine =
Machine<S,E,C>(
Machine(
State('green',
On<S,E,C>('entry',
On('entry',
Do(beginTimer),
Do(log)
),
On<S,E,C>('timer-finished',
On('timer-finished',
Goto('yellow')
)
),
State('yellow',
On<S,E,C>('entry',
On('entry',
Do(beginTimer),
Do(log)
),
On<S,E,C>('timer-finished',
On('timer-finished',
Goto('red')
)
),
State('red',
On<S,E,C>('entry',
On('entry',
Do(beginTimer),
Do(log)
),
On<S,E,C>('timer-finished',
On('timer-finished',
Goto('green')
)
),
+84
View File
@@ -0,0 +1,84 @@
import { Machine, State, On, Do, Goto, Spawn, Unspawn, interpret, Interpreter_T, send, Event_T } from '../index';
const wait = (ms:number)=>new Promise((resolve)=>{ setTimeout(()=>{ resolve(1); }, ms); });
const makeRequest = (ctx,e,self)=>{ send(ctx.serverActor, ['received-request',self]); };
const sendResponse = (ctx,e,self)=>{ send(ctx.clientActor, ['received-response',self]); };
const startTimer = async (ctx,e,self)=>{ await wait(500); send(self, ['timer-finished',null]); }
const log = (ctx, e, self:Interpreter_T)=>{ console.log(self.state); };
type Sc =
| 'idle'
| 'making-request'
| 'awaiting-response';
type Ec =
| ['entry',null]
| ['received-response',Interpreter_T]
| ['server-created', Interpreter_T];
type Cc =
| {serverActor?:Interpreter_T};
const client =
Machine(
State('idle',
On('entry',
Do(log),
),
On('server-created',
Do((_ctx,[_eventName,serverActor],self)=>{ self.context.serverActor=serverActor; }),
Goto('making-request')
)
),
State('making-request',
On('entry',
Do(log),
Do(makeRequest),
Goto('awaiting-response')
),
),
State('awaiting-response',
On('entry',
Do(log),
),
On('received-response',
Do(log),
Goto('making-request')
),
),
);
type Ss =
| 'awaiting-request'
| 'sending-response';
type Es =
| ['entry',null]
| ['received-request',Interpreter_T]
| ['timer-finished', null];
type Cs = {clientActor?:Interpreter_T};
const server =
Machine(
State('awaiting-request',
On('entry',
Do(log),
),
On('received-request',
Do((_ctx,[_eventName,clientActor],self)=>{ self.context.clientActor=clientActor; }),
Goto('sending-response')
),
),
State('sending-response',
On('entry',
Do(log),
Do(startTimer)
),
On('timer-finished',
Do(sendResponse),
Goto('awaiting-request')
)
),
);
const clientActor = interpret(client, {context:{}});
const serverActor = interpret(server, {context:{}});
send(clientActor, ['server-created', serverActor]);