Reducer

abstract class Reducer<State, Event, Effect>(source)

Similiar to React's useReducer hook, this contract defines a reducer that can be used to manage the state of a component. The reducer method is used to reduce an event to a new state and potentially produce a list of effects. This effect can be used to trigger side-effects, outside of the reducer, such as network requests, timers or updating the UI. A reducer should be used to manage the state of a component in a deterministic way (i.e., given the same sequence of events, the reducer should always produce the same state - pure function).

To alter the internal state of a component, callers can use dispatch to emit events. The current state can be consulted using the currentState method.

Parameters

State

the type of the state managed by the reducer.

Event

the type of the event that triggers a state transition.

Effect

the type of the effect emitted by the reducer.

Inheritors

Constructors

Link copied to clipboard
constructor()

Functions

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

Returns the current state of the reducer.

Link copied to clipboard
abstract suspend fun dispatch(event: Event)

Dispatches an event to the reducer to trigger a state transition.

Link copied to clipboard
protected abstract suspend fun reducer(state: State, event: Event): Pair<State, List<Effect>>

Given the current state and an event, reduces the event to a new state and a list of effects.