-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
218f2d6
commit 6ace584
Showing
8 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Subject, merge } from 'rxjs'; | ||
|
||
import { fsmHelpersFactory } from '../lib'; | ||
|
||
import { Input, State, initialState, transitions } from './state.config'; | ||
|
||
/** | ||
* Example 3 | ||
* Demonstrates creating a FSM using by the `fsm` operator. | ||
*/ | ||
|
||
const { | ||
markInput, | ||
fsm, | ||
} = fsmHelpersFactory<State, Input>(transitions); | ||
|
||
// Define inputs | ||
const toggle$ = new Subject<void>(); | ||
const hide$ = new Subject<void>(); | ||
|
||
// Use helper functions | ||
const input$ = merge( | ||
toggle$.pipe(markInput('toggle')), | ||
hide$.pipe(markInput('hide')), | ||
); | ||
|
||
const state$ = input$.pipe(fsm(initialState)); | ||
|
||
// Listen | ||
state$.subscribe((newState) => console.log('New state:', newState)); | ||
|
||
console.log('Initial state:', initialState); | ||
console.log('Input: toggle'); | ||
toggle$.next(); | ||
console.log('Input: toggle'); | ||
toggle$.next(); | ||
console.log('Input: toggle'); | ||
toggle$.next(); | ||
|
||
console.log('Input: hide'); | ||
hide$.next(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { markInput, nextState } from '../utils'; | ||
import { fsm, markInput, nextState } from '../utils'; | ||
import { BaseInput, BaseState, StateTransitions } from '../models'; | ||
|
||
/** | ||
* Factory for helper functions, independent of state machine instance. | ||
*/ | ||
export const fsmHelpersFactory = <S extends BaseState, T extends BaseInput>( | ||
transitions: StateTransitions<S, T>, | ||
) => ({ markInput: markInput<unknown, T>(), nextState: nextState<S, T>(transitions) }); | ||
) => ({ | ||
markInput: markInput<unknown, T>(), | ||
nextState: nextState<S, T>(transitions), | ||
fsm: fsm(transitions), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { pipe, scan, startWith } from 'rxjs'; | ||
import { BaseInput, BaseState, StateTransitions } from '../models'; | ||
import { nextState } from './next'; | ||
|
||
export const fsm = <S extends BaseState, T extends BaseInput>( | ||
transitions: StateTransitions<S, T>, | ||
) => ( | ||
initialState: S, | ||
) => pipe( | ||
scan(nextState<S, T>(transitions), initialState), | ||
startWith(initialState), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './mark-input'; | ||
export * from './next'; | ||
export * from './fsm'; |