Skip to content

Commit

Permalink
feat(demo/fsm2): rewrite with abstract for base class
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and AliMD committed Jun 5, 2023
1 parent 948ec30 commit beee9a2
Showing 1 changed file with 12 additions and 71 deletions.
83 changes: 12 additions & 71 deletions demo/finite-state-machine2/light-machine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {FiniteStateMachine} from '@alwatr/fsm2';
import {FiniteStateMachine, StateConfig} from '@alwatr/fsm2';
import {delay} from '@alwatr/util';

type States = 'green' | 'yellow' | 'red' | 'flashingRed';
type Events = 'POWER_LOST' | 'TIMER' | 'POWER_BACK';

const lightMachine = new FiniteStateMachine({
name: 'light-machine',
initial: 'green',
states: {
class LightMachine extends FiniteStateMachine<States, Events> {
constructor() {
super({name: 'light-machine', initial: 'green'});
}

statesRecord: StateConfig<States, Events> = {
$all: {
entry(): void {console.log('$all entry called');},
exit(): void {console.log('$all exit called');},
Expand Down Expand Up @@ -54,73 +58,10 @@ const lightMachine = new FiniteStateMachine({
},
},
},
},
});

// type LightMachine = FsmTypeHelper<typeof lightMachineConstructor>;

// // entries actions
// finiteStateMachineProvider.defineActions<LightMachine>('light_machine', {
// 'action_all_entry': (m): void => console.log('$all entry called', m.getState()),
// 'action_green_entry': (): void => console.log('green entry called'),
// 'action_yellow_entry': (): void => console.log('yellow entry called'),
// 'action_red_entry': (): void => console.log('red entry called'),
// 'action_flashingRed_entry': (): void => console.log('flashingRed entry called'),
// });

// // exits actions
// finiteStateMachineProvider.defineActions<LightMachine>('light_machine', {
// 'action_all_exit': (): void => console.log('$all exit called'),
// 'action_green_exit': (): void => console.log('green exit called'),
// 'action_yellow_exit': (): void => console.log('yellow exit called'),
// 'action_red_exit': (): void => console.log('red exit called'),
// 'action_flashingRed_exit': (): void => console.log('flashingRed exit called'),
// });

// // transition events actions
// finiteStateMachineProvider.defineActions<LightMachine>('light_machine', {
// 'action_all_POWER_LOST': (): void => console.log('$all.POWER_LOST actions called'),
// 'action_green_TIMER': (): void => console.log('green.TIMER actions called'),
// 'action_yellow_TIMER': (): void => console.log('yellow.TIMER actions called'),
// 'action_red_TIMER': (): void => console.log('red.TIMER actions called'),
// 'action_flashingRed_POWER_BACK': (): void => console.log('flashingRed.POWER_BACK actions called'),
// });

// finiteStateMachineProvider.defineSignals<LightMachine>('light_machine', [
// {
// signalId: 'new_content_received',
// transition: 'POWER_BACK',
// contextName: 'a',
// receivePrevious: 'NextCycle',
// },
// ]);

// // signals
// // 'action_ali_signal': (a): void => console.log('ali signal ', a),

// // Consumer
// const lightMachineConsumer = finiteStateMachineConsumer<LightMachine>('light_machine-50', 'light_machine');
};
}

// lightMachineConsumer.defineSignals([
// {
// signalId: 'power_button_click_event',
// transition: 'POWER_BACK',
// receivePrevious: 'No',
// },
// {
// signalId: 'jafang',
// callback: (signalDetail: Record<string, string>): void => {
// console.log(signalDetail);
// },
// receivePrevious: 'NextCycle',
// },
// {
// callback: (): void => {
// console.log('subscribe_callback', lightMachineConsumer.getState());
// },
// receivePrevious: 'NextCycle',
// },
// ]);
const lightMachine = new LightMachine();

console.log('start', lightMachine.state);

Expand Down

0 comments on commit beee9a2

Please sign in to comment.