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 Reducer
s and Effects
.
An Environment
can be set up to enable dependency injection in Effect
s.
Usage
To update the State
callers dispatch Action
s 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 Action
s and State
changes by registering an Interceptor
.
-
The environment passed to the
Effects
. TheEnvironment
can contain services and other dependencies.Declaration
Swift
public let environment: Environment
-
Dispatches an
Action
and creates a newState
by running the currentState
and theAction
through all registeredReducer
s.After the
State
is set, all registeredInterceptor
s are notified of the change. Lastly theAction
is dispatched to all registeredEffect
s.Declaration
Swift
public func dispatch(action: Action)
Parameters
action
The
Action
to dispatch
-
Declaration
Swift
public func register(effects: [Effect<Environment>], id: String = "*")
-
Registers the given
Interceptor
. TheInterceptor
will receive all subsequentAction
s and state changes.Declaration
Swift
public func register<I>(interceptor: I) where State == I.State, I : Interceptor
Parameters
interceptor
The
Interceptor
to register -
Unregisters all registered
Interceptor
s of the given type. TheInterceptor
s will no longer receive anyAction
s or state changes.Declaration
Swift
public func unregisterInterceptors<I>(ofType interceptor: I.Type) where State == I.State, I : Interceptor
Parameters
interceptor
The type of
Interceptor
to unregister
-
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>)
-
Creates a
Binding
from the givenSelector
andActionTemplate
.When the
wrappedValue
is updated anAction
, created from theActionTemplate
, is dispatched on theStore
.Declaration
Swift
func binding<Value>(get selector: Selector<State, Value>, send actionTemplate: ActionTemplate<Value>) -> Binding<Value>
Parameters
selector
The
Selector
s to use for getting the current valueactionTemplate
The
ActionTemplate
to use for dispatching anAction
when the value changesReturn Value
A
Binding
based on the givenSelector
andActionTemplate
-
Creates a
Binding
from the givenSelector
andActionTemplate
s for enabling and disabling the value.When the
wrappedValue
is enabled/disabled, anAction
, created from one of theActionTemplate
s, is dispatched on theStore
.Declaration
Swift
func binding(get selector: Selector<State, Bool>, enable enableActionTemplate: ActionTemplate<Void>, disable disableActionTemplate: ActionTemplate<Void>) -> Binding<Bool>
Parameters
selector
The
Selector
s to use for getting the current valueenableActionTemplate
The
ActionTemplate
to use for dispatching anAction
when the value should be enableddisableActionTemplate
The
ActionTemplate
to use for dispatching anAction
when the value should be disabledReturn Value
A
Binding
based on the givenSelector
andActionTemplate
s -
Creates a
Binding
from the givenSelector
and closure.When the
wrappedValue
is updated anAction
(returned by the closure), is dispatched on theStore
.Declaration
Parameters
selector
The
Selector
s to use for getting the current valueaction
A closure which returns an
Action
to be dispatched when the value changesvalue
The value used to decide which
Action
to be dispatched.Return Value
A
Binding
based on the givenSelector
and closure
-
Initializes the
Store
with an initialState
and eventuallyReducer
s.Using this initializer will give all
Effects
aVoid
environment.Declaration
Swift
convenience init(initialState: State, reducers: [Reducer<State>] = [])
Parameters
initialState
The initial
State
for theStore
reducers
The
Reducer
s to register