Dex.StateMachine

g Dex.Object Dex.Object Dex.StateMachine Dex.StateMachine Dex.Object->Dex.StateMachine

Subclasses:

None

Methods

Inherited:

Dex.Object (2)

get_requested_state ()

get_state ()

interrupt ()

transition (target)

wait_for_state (state)

Virtual Methods

None

Fields

None

Class Details

class Dex.StateMachine
Bases:

Dex.Object

Abstract:

No

DexStateMachine provides 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:

int

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.

get_state()
Returns:

the current state

Return type:

int

Gets the current state of self.

interrupt()
Returns:

True if an active transition context was marked interrupted; otherwise False

Return type:

bool

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:

Dex.Future

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:

Dex.Future

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.