ColdStateRepo

abstract class ColdStateRepo<State : KalugaState>(coroutineContext: CoroutineContext = Dispatchers.Main.immediate, val lazyMutableFlow: Lazy<MutableSharedFlow<State>> = defaultLazySharedFlow()) : BaseColdStateRepo<State, MutableSharedFlow<State>>

A BaseColdStateRepo that represents its State as a Cold flow. Data will only be set when the state is observed.

This class uses a very simple initialize and deinitialize pattern without changes of state by default

This implementation uses a MutableSharedFlow. If you want to use a cold state repo based on StateFlow, consider ColdStateFlowRepo

Parameters

State

the type of KalugaState represented by this repo.

coroutineContext

the CoroutineContext used to create a coroutine scope for this state machine. Make sure that if you pass a coroutine context that has sequential execution if you do not want simultaneous state changes. The default Main dispatcher meets these criteria.

lazyMutableFlow

A Lazy of State in which to store the backing data

See also

Constructors

Link copied to clipboard
constructor(coroutineContext: CoroutineContext = Dispatchers.Main.immediate, lazyMutableFlow: Lazy<MutableSharedFlow<State>> = defaultLazySharedFlow())

Properties

Link copied to clipboard
Link copied to clipboard
open override val lazyMutableFlow: Lazy<MutableSharedFlow<State>>
Link copied to clipboard
val subscriptionCount: StateFlow<Int>

Number of subscribers to the state repo

Functions

Link copied to clipboard
open suspend override fun collect(collector: FlowCollector<State>): Nothing
Link copied to clipboard
inline suspend fun <T> Flow<T>.collectImportant(collector: FlowCollector<T>)

Collects all elements not implementing SpecialFlowValue.NotImportant from a Flow.

Link copied to clipboard
inline suspend fun <T> Flow<T>.collectImportantUntilLast(includeLast: Boolean = true, collector: FlowCollector<T>)

Collects all elements not implementing SpecialFlowValue.NotImportant from a Flow up to the first SpecialFlowValue.Last received.

Link copied to clipboard
inline suspend fun <T> Flow<T>.collectUntilLast(includeLast: Boolean = true, collector: FlowCollector<T>)

Collects all elements from a Flow up to the first SpecialFlowValue.Last received.

Link copied to clipboard
abstract suspend fun deinitialize(state: State)

Called when the repo is no longer observed

Link copied to clipboard
inline fun <T> Flow<T>.filterOnlyImportant(): Flow<T>

Filters out all values marked as SpecialFlowValue.NotImportant from a given Flow

Link copied to clipboard
suspend override fun firstCollection()
Link copied to clipboard
inline suspend fun <R> Flow<*>.firstInstance(): R?

Returns first element that is instance of specific type parameter R, or null if not found

Link copied to clipboard
abstract suspend fun initialValue(): State

Gets the initial value of the repo

Link copied to clipboard
suspend override fun laterCollections(): State
Link copied to clipboard
fun launchTakeAndChangeState(context: CoroutineContext = coroutineContext, action: suspend (State) -> suspend () -> State): Job
fun <K : State> launchTakeAndChangeState(context: CoroutineContext = coroutineContext, remainIfStateNot: KClass<K>, action: suspend (K) -> suspend () -> State): Job

Launches in a given CoroutineContext to change from the current State to a new State. This operation ensures atomic state changes.

Link copied to clipboard
fun <Result> launchUseState(context: CoroutineContext = coroutineContext, action: suspend (State) -> Result): Job

Launches in a given CoroutineContext and calls action on the current State. The state is guaranteed not to change during the execution of action. This operation ensures atomic state observations, so the state will not change while the action is being executed.

Link copied to clipboard
suspend override fun noMoreCollections(): State
Link copied to clipboard

Peek the current state of the state machine. The current state could change immediately after it is returned.

Link copied to clipboard
suspend fun takeAndChangeState(action: suspend (State) -> suspend () -> State): State
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.

Link copied to clipboard
fun <T> Flow<T>.takeUntilLast(includeLast: Boolean = true): Flow<T>

Transforms a Flow to emit only up to the first SpecialFlowValue.Last received.

Link copied to clipboard
suspend fun <Result> useState(action: suspend (State) -> Result): Result

Makes the current State available in action. The state is guaranteed not to change during the execution of action. This operation ensures atomic state observations, so the state will not change while the action is being executed.