Store

open class Store<State, Environment> : ObservableObject
extension Store: Subscriber

The Store is a centralized container for a single-source-of-truth State.

A Store is configured by registering all the desired Reducers and Effects.

An Environment can be set up to enable dependency injection in Effects.

Usage

To update the State callers dispatch Actions on the Store.

Selecting

To select a value in the State the callers can either use a Selector or a key path. It is possible to get a Publisher for the value or just to select the current value.

Interceptors

It is possible to intercept all Actions and State changes by registering an Interceptor.

  • The state of the Store. It can only be modified by the registered Reducers when Actions are dispatched.

    Declaration

    Swift

    @Published
    public private(set) var state: State { get set }
  • The environment passed to the Effects. The Environment can contain services and other dependencies.

    Declaration

    Swift

    public let environment: Environment

Initialization

  • Initializes the Store with an initial State, an Environment and eventually Reducers.

    Declaration

    Swift

    public init(initialState: State, environment: Environment, reducers: [Reducer<State>] = [])

    Parameters

    initialState

    The initial State for the Store

    environment

    The Environment to pass to Effects

    reducers

    The Reducers to register

Dispatching

  • Dispatches an Action and creates a new State by running the current State and the Action through all registered Reducers.

    After the State is set, all registered Interceptors are notified of the change. Lastly the Action is dispatched to all registered Effects.

    Declaration

    Swift

    public func dispatch(action: Action)

    Parameters

    action

    The Action to dispatch

Reducers

  • Registers the given Reducer. The Reducer will be run for all subsequent actions.

    Declaration

    Swift

    public func register(reducer: Reducer<State>)

    Parameters

    reducer

    The Reducer to register

  • Registers the given Reducer for a slice of the State. The Reducer will be run for all subsequent actions.

    Declaration

    Swift

    public func register<Substate>(reducer: Reducer<Substate>, for keyPath: WritableKeyPath<State, Substate>)

    Parameters

    reducer

    The Reducer to register

    keyPath

    The KeyPath for which the Reducer should be run

  • Unregisters the given Reducer. The Reducer will no longer be run when Actions are dispatched.

    Declaration

    Swift

    public func unregister<SomeState>(reducer: Reducer<SomeState>)

    Parameters

    reducer

    The Reducer to unregister

Effects

  • Registers the given Effects. The Effects will receive all subsequent actions.

    Declaration

    Swift

    public func register<E>(effects: E) where Environment == E.Environment, E : Effects

    Parameters

    effects

    The Effects to register

  • Registers the given Effects. The Effects will receive all subsequent actions.

    Declaration

    Swift

    public func register(effects: [Effect<Environment>])

    Parameters

    effects

    The array of Effects to register

  • Registers the given Effect. The Effect will receive all subsequent actions.

    Only Effects registered from a type conforming to Effects can be unregistered.

    Declaration

    Swift

    public func register(effect: Effect<Environment>)

    Parameters

    effect

    The Effect to register

  • Unregisters the given Effects. The Effects will no longer receive any actions.

    Declaration

    Swift

    public func unregisterEffects<E>(ofType effects: E.Type) where Environment == E.Environment, E : Effects

    Parameters

    effects

    The Effects to register

Interceptors

Selecting

  • Creates a Publisher for a Selector.

    Declaration

    Swift

    open func select<Value>(_ selector: Selector<State, Value>) -> AnyPublisher<Value, Never>

    Parameters

    selector

    The Selector to use when getting the value in the State

    Return Value

    A Publisher for the Value in the State

  • Gets the current value in the State for a Selector.

    Declaration

    Swift

    open func selectCurrent<Value>(_ selector: Selector<State, Value>) -> Value

    Parameters

    selector

    The Selector to use when getting the value in the State

    Return Value

    The current Value in the State

Subscriptions

Available where Environment == Void

  • Initializes the Store with an initial State and eventually Reducers.

    Using this initializer will give all Effects a Void environment.

    Declaration

    Swift

    convenience init(initialState: State, reducers: [Reducer<State>] = [])

    Parameters

    initialState

    The initial State for the Store

    reducers

    The Reducers to register