Skip to content

@ActionState

Action state decorator. Temporarily enters a specified state during async execution, returns to the original state after.

Signature

function ActionState(name?: string): MethodDecorator

Parameters

ParameterDescription
nameTemporary state name. Defaults to the method name if omitted.

Behavior

class Doc extends FSM {
@ActionState('saving')
async save() {
await persist()
}
}

Running save():

  1. Record old = this.state
  2. setState('saving') (note: directly to 'saving', not a MiddleState)
  3. Run the original method
    • Success: setState(old), return the result
    • Failure: setState(old, err), throw the original error

vs @ChangeState

@ChangeState@ActionState
Pre-execution validationvalidates fromnone
Post-execution statenew stable state toreturns to original
IntermediateMiddleState (${action}ing)directly name
In stateDiagramyesno
Failure rollbackto fromto original

Sync method support

If the original method returns a sync value, behavior is the same (sync return, state restored):

@ActionState('refreshing')
refresh() {
return this.cache
}

Example

The dataFetch example uses @ActionState('retrying') to surface the retry-wait intermediate state.

See also