StateActor

open class StateActor<Context, S>(initial: S, scope: CoroutineScope) : StateStore<S> , Actor<Context, S> (source)

Holds state and provides synchronous updates + async action dispatching.

update uses MutableStateFlow.update internally (CAS retry) — concurrent updates from multiple actions are safe.

Dispatch modes:

  • effect: fire-and-forget — launches in the actor's scope.

  • immediateUntil: dispatch + suspend until state matches a condition.

Interceptors

actor.onUpdate { reducer, next ->
next(reducer) // call to proceed, skip to suppress
}

actor.onAction { action, next ->
next() // call to proceed, skip to suppress
}

Inheritors

Constructors

Link copied to clipboard
constructor(initial: S, scope: CoroutineScope)

Properties

Link copied to clipboard
open override val state: StateFlow<S>

Functions

Link copied to clipboard
open override fun <Ctx> effect(ctx: Ctx, action: TypedAction<Ctx>)

Fire-and-forget: launch action in actor's scope, routed through interceptors.

Link copied to clipboard
open suspend override fun <Ctx> immediate(ctx: Ctx, action: TypedAction<Ctx>)

Dispatch action inline and suspend until it completes. Goes through action interceptors. Use for cross-slice coordination where the caller needs to await the action finishing.

Link copied to clipboard
open suspend override fun <Ctx> immediateUntil(ctx: Ctx, action: TypedAction<Ctx>, until: (S) -> Boolean): S

Dispatch action and suspend until state matches until.

Link copied to clipboard
fun onAction(interceptor: (action: Any, next: () -> Unit) -> Unit)

Add an action interceptor. Call next() to proceed, or skip to suppress the action. Action is Any — cast to inspect.

Link copied to clipboard
fun onActionExecution(interceptor: suspend (action: Any, next: suspend () -> Unit) -> Unit)

Add an async interceptor that wraps the action's suspend execution. Runs inside the coroutine — next() suspends until the action completes.

Link copied to clipboard
fun onUpdate(interceptor: (reducer: Reducer<S>, next: (Reducer<S>) -> Unit) -> Unit)

Add an update interceptor. Call next(reducer) to proceed, or skip to suppress the update.

Link copied to clipboard
open override fun update(reducer: Reducer<S>)

Atomic state mutation using CAS retry, routed through update interceptors.