Store

@MainActor
public final class Store<State, Event, Environment>

A Store is the core of the application. It is used to manage state and handle events sent to it.

Generally an application will have one store and then use the scope function to create sub stores for different components of the app.

  • The state of the store.

    Declaration

    Swift

    public private(set) var state: State { get set }
  • A sequence that emits state changes.

    Declaration

    Swift

    public let stateSequence: AnyAsyncSequenceable<State>

Initialization

  • Construct a Store with state, reducer and environment.

    Declaration

    Swift

    public convenience init(
        state: State,
        reducer: Reducer<State, Event, Environment>,
        environment: Environment
    )

    Parameters

    state

    An initial state.

    reducer

    A reducer.

    environment

    An environment.

Events

  • Send an event through the store’s reducer.

    Declaration

    Swift

    public func send(_ event: Event)

    Parameters

    event

    The event.

Scope

  • Create a sub store from the current store.

    The scoped store derives it’s state and environment from the parent store. Events that are sent to this store are converted a parent store event, using the fromScopedEvent parameter and then passed to the parent store. Changes to the parent state are then reflected back to the scoped store.

    Declaration

    Swift

    public func scope<ScopedState, ScopedEvent, ScopedEnvironment>(
        state toScopedState: @escaping (_ state: State) -> ScopedState,
        event fromScopedEvent: @escaping (_ event: ScopedEvent) -> Event,
        environment toScopedEnvironment: (_ environment: Environment) -> ScopedEnvironment
    ) -> Store<ScopedState, ScopedEvent, ScopedEnvironment>

    Parameters

    state

    An initial state.

    event

    A reducer.

    environment

    An environment.

    Return Value

    A Store instance.