Dex.StateMachine¶
- Subclasses:
None
Methods¶
- Inherited:
|
|
|
|
|
|
|
Virtual Methods¶
None
Fields¶
None
Class Details¶
- class Dex.StateMachine¶
- Bases:
- Abstract:
No
DexStateMachineprovides a serialized asynchronous state machine.Transitions are declared up front with a static table of [struct`Dex`.StateTransition] entries. Requests made with [method`Dex`.StateMachine.transition] are serialized through an internal [class`Dex`.Limiter] with a max concurrency of one. Transition callbacks run from a fiber, so they may use
dex_await()and related APIs while still appearing as synchronous functions.- get_requested_state()¶
- Returns:
the most recent valid requested state
- Return type:
Gets the most recent valid state requested with [method`Dex`.StateMachine.transition].
The requested state is initialized to the initial state passed to [ctor`Dex`.StateMachine.new]. It is updated immediately when [method`Dex`.StateMachine.transition] is called with a valid target state, before that transition is run. It is not a guarantee that the transition will run or succeed, and it may differ from the current state returned by [method`Dex`.StateMachine.get_state].
Added in version 1.2.
- interrupt()¶
-
Cooperatively interrupts the active transition callback.
If a transition callback is currently active, its interrupt future returned by [method`Dex`.StateTransitionContext.wait_for_interrupt] is resolved. This does not request a transition or change the current state; the active callback decides how to handle the interrupt.
Added in version 1.2.
- transition(target)¶
- Parameters:
target (
int) – the target state- Returns:
a future resolving to the final enum state
- Return type:
Requests a transition to target.
If target is a valid value for the state machine’s enum type, it becomes the requested state immediately and can be read with [method`Dex`.StateMachine.get_requested_state]. The requested state is the most recent valid target passed to this method. It may differ from the current state and does not imply that the transition will succeed.
Transition requests are serialized. The matching callback is run from a fiber and may use
dex_await()to wait for asynchronous work. If the callback succeeds without calling [method`Dex`.StateTransitionContext.set_state], the state machine commits target. If the callback updates the state directly, the returned future resolves to the current state after the callback returns.
- wait_for_state(state)¶
- Parameters:
state (
int) – the state to wait for- Returns:
a future resolving to state
- Return type:
Waits until self enters state.
If self is already in state, the returned future is already resolved. Otherwise, the future resolves the next time the current state is set to state. This does not request a transition; use [method`Dex`.StateMachine.transition] to move the state machine.
Added in version 1.2.