ScreenStore

The state and the effects of one screen.

An EventRouter creates one store, keeps it for as long as the router lives and hands it to every EventHandler it runs, so all the handlers of a screen read and write the same state. The UI never receives the store: it reads EventRouter.state and EventRouter.effects.

Create one yourself only in a test, to call a handler directly:

val store = ScreenStore<TaskListUiState, TaskListEffect>(TaskListUiState.Loading)

OnRefreshHandler(FakeTaskRepository()).handle(OnRefresh, store)

Every member is safe to call from any thread.

Constructors

Link copied to clipboard
constructor(initialState: S)

Creates the store of one screen.

Properties

Link copied to clipboard
val current: S

The state of the screen right now.

Link copied to clipboard
val effects: Flow<F>

The effects of the screen, in the order they were emitted.

Link copied to clipboard
val state: StateFlow<S>

The state of the screen. It always has a value, and a new collector gets the latest one first.

Functions

Link copied to clipboard
fun emit(effect: F)

Sends effect to the UI. It never suspends and never fails.

Link copied to clipboard
fun update(transform: (S) -> S)

Replaces the state with the result of transform, which receives the current state.

Link copied to clipboard
suspend fun <T> updateOptimistically(stateUpdate: (S) -> S, stateIfFailed: (S) -> S, work: suspend () -> T): T

Changes the state at once, runs work, and puts the state back if the work does not finish.