-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
2 changed files
with
144 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type {MaybeArray, MaybePromise, StringifyableRecord} from '@alwatr/type'; | ||
|
||
|
||
export interface FsmConfig<TState extends string, TEventId extends string, TContext extends StringifyableRecord> { | ||
/** | ||
* Machine ID (It is used in the state change signal identifier, so it must be unique). | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Initial state. | ||
*/ | ||
initial: TState; | ||
|
||
/** | ||
* Initial context. | ||
*/ | ||
context: TContext; | ||
|
||
/** | ||
* Define state list | ||
*/ | ||
stateRecord: { | ||
[S in TState | '$all']: { | ||
/** | ||
* On state exit actions | ||
*/ | ||
exit?: MaybeArray<() => MaybePromise<void>>; | ||
|
||
/** | ||
* On state entry actions | ||
*/ | ||
entry?: MaybeArray<() => MaybePromise<void>>; | ||
|
||
/** | ||
* An object mapping eventId to state. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* stateRecord: { | ||
* on: { | ||
* TIMER: { | ||
* target: 'green', | ||
* condition: () => car.gas > 0, | ||
* actions: () => car.go(), | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
on: { | ||
[E in TEventId]?: TransitionConfig<TState>; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export interface StateContext<TState extends string, TEventId extends string> { | ||
[T: string]: string | undefined; | ||
/** | ||
* Current state | ||
*/ | ||
target: TState; | ||
/** | ||
* Last state | ||
*/ | ||
from: TState; | ||
/** | ||
* Transition event | ||
*/ | ||
by: TEventId | 'INIT'; | ||
} | ||
|
||
export interface TransitionConfig<TState extends string> { | ||
target?: TState; | ||
condition?: () => MaybePromise<boolean>; | ||
actions?: MaybeArray<() => MaybePromise<void>>; | ||
} |