takeAndChangeState

suspend fun takeAndChangeState(action: suspend (State) -> suspend () -> State): State

Changes from the current State to a new State. This operation ensures atomic state changes. The new state is determined by an action, which takes the current State upon starting the state transition and provides a deferred state creation. You are strongly encouraged to use the State provided by the action to determine the new state, to ensure no illegal state transitions occur, as the state may have changed between calling doTakeAndChangeState and the execution of action. If the action returns KalugaState.remain no state transition will occur. Since this operation is atomic, the action should not directly call doTakeAndChangeState itself. If required to do this, handle the additional transition in a separate coroutine.

This method uses a separate coroutineScope, meaning it will suspend until all child Jobs are completed, including those that asynchronously call this method itself.

Parameters

action

Function to determine the State to be transitioned to from the current State. If no state transition should occur, return KalugaState.remain


suspend fun <K : State> takeAndChangeState(remainIfStateNot: KClass<K>, action: suspend (K) -> suspend () -> State): State

Changes from the current State to a new State. This operation ensures atomic state changes. The new state is determined by an action, which takes the current State upon starting the state transition and provides a deferred state creation. You are strongly encouraged to use the State provided by the action to determine the new state, to ensure no illegal state transitions occur, as the state may have changed between calling doTakeAndChangeState and the execution of action. If the action returns KalugaState.remain no state transition will occur. Since this operation is atomic, the action should not directly call doTakeAndChangeState itself. If required to do this, handle the additional transition in a separate coroutine.

This method uses a separate coroutineScope, meaning it will suspend until all child Jobs are completed, including those that asynchronously call this method itself.

Parameters

remainIfStateNot

If the current state at the time of Action is not an instance of this class, the state will automatically remain.

action

Function to determine the State to be transitioned to from the current State. If no state transition should occur, return KalugaState.remain