Store

interface Store<out State> : DispatchScope<State>

This is an equivalent of Redux store. However, there are a few differences:

  • Instead of getState there is a currentState property that returns current state.

  • State changes are received by collecting state.

  • Contains closure field that provides a mechanism to inject objects to the store and make them available to middlewares.

To create an instance use buildStore (recommended) or Store() function.

Functions

Link copied to clipboard
abstract fun dispatch(action: Action)

This is equivalent to Redux dispatch.

Properties

Link copied to clipboard
abstract val closure: DispatchClosure

There is no equivalent to this in original Redux. It provides a current DispatchClosure. It remains immutable. However, elements inside might mutate depending on their implementation.

Link copied to clipboard
abstract val currentState: State

This is equivalent to Redux getState.

Link copied to clipboard
abstract val state: StateFlow<State>

Extensions

Link copied to clipboard
val DispatchScope<*>.coroutineScope: CoroutineScope

Returns CoroutineScope associated with a store.

Link copied to clipboard
Link copied to clipboard

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. Coroutine is launched in a scope provided by DispatchCoroutineScope.

Link copied to clipboard
fun DispatchScope<*>.dispatchJobIn(action: ForegroundJobAction, scope: CoroutineScope): Job

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. Coroutine is launched in the scope.

Link copied to clipboard

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. This function suspends until foreground job is finished. When coroutine that calls this function is cancelled, foreground job is also cancelled.

Link copied to clipboard

Maps Store.state to a new StateFlow using given selector function. selector function is not called before selected state is actually accessed using StateFlow.value or collected. Also, it's not called if state is not changed. State is compared using equals so selection might be recalculated even if unrelated part of the state changed. However, if selection stays the same (again compared using equals), it's not emitted (just like every StateFlow). To optimize state selection, use select with Selector param.

fun <State, Selected> Store<State>.select(selector: Selector<State, Selected>): StateFlow<Selected>

Maps Store.state to a new StateFlow using given selector. Selector function is called only if Selector.isStateEqual returns false (by default it refers to equals). Also, selector function is not called before selected state is actually accessed using StateFlow.value or collected. Selector result is emitted only if Selector.isSelectionEqual returns false (by default it refers to equals).

Link copied to clipboard
fun DispatchScope<*>.synchronized(block: suspend DispatchScope<*>.() -> Unit): Job

Runs given block as a coroutine in DispatchScope.coroutineScope. Effectively it ensures that any operation on a DispatchScope inside a block runs on proper thread.

Link copied to clipboard
inline fun <T> DispatchScope<*>.withLocalClosure(closure: DispatchClosure, newFrame: Boolean = false, block: DispatchScope<*>.() -> T): T

Adds local changes to LocalClosureContainer with a given closure for a time of block execution.