Skip to content

Commit

Permalink
feat(demo/fsm): new demo for state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Mar 17, 2023
1 parent c8d91eb commit 69e4661
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions demo/finite-state-machine/light-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,70 @@ const lightMachine = new FiniteStateMachine({
a: <number> 0,
b: <number> 0,
},
states: {
stateRecord: {
$all: {
entry: (): void => console.log('$all entry called'),
exit: (): void => console.log('$all exit called'),
on: {
POWER_LOST: 'flashingRed',
POWER_LOST: {
target: 'flashingRed',
actions: (): void => console.log('$all.POWER_LOST actions called'),
},
},
},
green: {
entry: (): void => console.log('green entry called'),
exit: (): void => console.log('green exit called'),
on: {
TIMER: 'yellow',
TIMER: {
target: 'yellow',
actions: (): void => console.log('green.TIMER actions called'),
},
},
},
yellow: {
entry: (): void => console.log('yellow entry called'),
exit: (): void => console.log('yellow exit called'),
on: {
TIMER: 'red',
TIMER: {
target: 'red',
actions: (): void => console.log('yellow.TIMER actions called'),
},
},
},
red: {
entry: (): void => console.log('red entry called'),
exit: (): void => console.log('red exit called'),
on: {
TIMER: 'green',
TIMER: {
target: 'green',
actions: (): void => console.log('red.TIMER actions called'),
},
},
},
flashingRed: {
entry: (): void => console.log('flashingRed entry called'),
exit: (): void => console.log('flashingRed exit called'),
on: {
POWER_BACK: 'green',
POWER_BACK: {
target: 'green',
actions: (): void => console.log('flashingRed.POWER_BACK actions called'),
},
},
},
},
});


lightMachine.signal.subscribe((state) => {
console.log('****\nstate: %s, context: %s\n****', state, lightMachine.context);
}, {receivePrevious: 'No'});

lightMachine.transition('TIMER', {a: 1});
lightMachine.transition('TIMER', {b: 2});
lightMachine.transition('TIMER');
lightMachine.transition('POWER_LOST', {a: 4});
lightMachine.transition('TIMER', {a: 5, b: 5});
lightMachine.transition('POWER_BACK', {a: 6});
console.log('start');

await lightMachine.transition('TIMER', {a: 1});
await lightMachine.transition('TIMER', {b: 2});
await lightMachine.transition('TIMER');
await lightMachine.transition('POWER_LOST', {a: 4});
await lightMachine.transition('TIMER', {a: 5, b: 5});
await lightMachine.transition('POWER_BACK', {a: 6});

0 comments on commit 69e4661

Please sign in to comment.