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>], id: String = "*")

    Parameters

    effects

    The array of Effects to register

    id

    The identifier for the Effects. Only used to enable unregistering the Effects later

  • 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>, id: String = "*")

    Parameters

    effect

    The Effect to register

    id

    The identifier for the Effect. Only used to enable unregistering the Effect later

  • 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

  • Unregisters the Effects registered with the id, so they will no longer receive any actions.

    Declaration

    Swift

    public func unregisterEffects(withId id: String)

    Parameters

    id

    The identifier used to register the Effects

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

  • Declaration

    Swift

    public typealias Input = Action
  • Declaration

    Swift

    public typealias Failure = Never
  • Declaration

    Swift

    public func receive(subscription: Subscription)
  • Declaration

    Swift

    public func receive(_ input: Action) -> Subscribers.Demand
  • Declaration

    Swift

    public func receive(completion _: Subscribers.Completion<Never>)

SwiftUI bindings

  • Creates a Binding from the given Selector and ActionTemplate.

    When the wrappedValue is updated an Action, created from the ActionTemplate, is dispatched on the Store.

    Declaration

    Swift

    func binding<Value>(get selector: Selector<State, Value>,
                        send actionTemplate: ActionTemplate<Value>) -> Binding<Value>

    Parameters

    selector

    The Selectors to use for getting the current value

    actionTemplate

    The ActionTemplate to use for dispatching an Action when the value changes

    Return Value

    A Binding based on the given Selector and ActionTemplate

  • Creates a Binding from the given Selector and ActionTemplates for enabling and disabling the value.

    When the wrappedValue is enabled/disabled, an Action, created from one of the ActionTemplates, is dispatched on the Store.

    Declaration

    Swift

    func binding(get selector: Selector<State, Bool>,
                 enable enableActionTemplate: ActionTemplate<Void>,
                 disable disableActionTemplate: ActionTemplate<Void>)
        -> Binding<Bool>

    Parameters

    selector

    The Selectors to use for getting the current value

    enableActionTemplate

    The ActionTemplate to use for dispatching an Action when the value should be enabled

    disableActionTemplate

    The ActionTemplate to use for dispatching an Action when the value should be disabled

    Return Value

    A Binding based on the given Selector and ActionTemplates

  • Creates a Binding from the given Selector and closure.

    When the wrappedValue is updated an Action (returned by the closure), is dispatched on the Store.

    Declaration

    Swift

    func binding<Value>(get selector: Selector<State, Value>,
                        send action: @escaping (_ value: Value) -> Action)
        -> Binding<Value>

    Parameters

    selector

    The Selectors to use for getting the current value

    action

    A closure which returns an Action to be dispatched when the value changes

    value

    The value used to decide which Action to be dispatched.

    Return Value

    A Binding based on the given Selector and closure

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