MiddleState
An intermediate transition state. Represents “in the process of going from oldState via action toward newState”.
Definition
export class MiddleState { oldState: State newState: string action: string aborted: boolean = false
constructor(oldState: State, newState: string, action: string) abort(fsm: IFSM): void toString(): string // returns `${action}ing`}Fields
| Field | Description |
|---|---|
oldState | starting state |
newState | target state (the stable state after success) |
action | action name (defaults to the decorated method name; overridable via opt.action) |
aborted | whether it has been aborted |
toString
const m = new MiddleState('idle', 'done', 'fetch')m.toString() // 'fetching'The state property returns this MiddleState instance during the intermediate phase, but state.toString() always returns the string form.
abort
abort(fsm: IFSM): voidAborts this intermediate state:
- Sets
aborted = true - Calls
setState.call(fsm, oldState, new Error("action '{action}' aborted")) - State rolls back to
oldState;stateChangedevent fires (with error)
After abort, if the original method is still running, its success callback checks middle.aborted and skips setState(to) if aborted.
When it appears
Created automatically when a @ChangeState-decorated method is called:
@ChangeState('idle', 'done')async fetch() { // here this.state is MiddleState('idle', 'done', 'fetch') // this.state.toString() === 'fetching'}Listening to intermediate states
Intermediate states dispatch an event with name ${action}ing:
obj.on('fetching', (oldState) => { console.log('fetch started, old state', oldState)})They also appear as the newState parameter of the stateChanged event (as a MiddleState instance).