-
Notifications
You must be signed in to change notification settings - Fork 2
Reducer
Define an EffectID
namespace for this reducer and all its descendents.
public func namespace<Namespace: Hashable>(_ namespace: Namespace) -> Self
Namespaces allow to discern effects emanating from different instances of the same Store
type running at the same time in the same process. This can happen with document-based apps
where each document is supported by distinct instances of Store
, each one without knowledge
of the other ones. Without namespacing, the Store
for the document "A" may cancel some
ongoing Effect
s from the document "B" Store
.
You ideally define a namespace for the Reducer
of the root store, using some Hashable
value that is unique to the Store
instance, like a document identifier.
- namespace: some
Hashable
value that is unique to the store using this reducer.
A reducer that defines a namespace for identifiers defined with the @EffectID
property wrapper.
Define an EffectID
namespace for this reducer and all its descendents using a constant
identifier extracted from State
.
public func namespace<ID: Hashable>(_ id: @escaping (State) -> ID) -> Self
Namespaces allow to discern effects emanating from different instances of the same Store
type running at the same time in the same process. This can happen with document-based apps
where each document is supported by distinct instances of Store
, each one without knowledge
of the other ones. Without namespacing, the Store
for the document "A" may cancel some
ongoing Effect
s from the document "B" Store
.
You ideally define a namespace for the Reducer
of the root store, using some Hashable
value that is unique to the Store
instance, like a document identifier.
- id: some
Hashable
value derived fromState
that is constant and unique to the store using this reducer. You can use any function, or aKeyPath<State, ID>
.
A reducer that defines a namespace for identifiers defined with the @EffectID
property wrapper.
Define an EffectID
namespace for this reducer and all its descendents using a constant
identifier extracted from the Environment
.
public func namespace<ID: Hashable>(_ id: @escaping (Environment) -> ID) -> Self
Namespaces allow to discern effects emanating from different instances of the same Store
type running at the same time in the same process. This can happen with document-based apps
where each document is supported by distinct instances of Store
, each one without knowledge
of the other ones. Without namespacing, the Store
for the document "A" may cancel some
ongoing Effect
s from the document "B" Store
.
You ideally define a namespace for the Reducer
of the root store, using some Hashable
value that is unique to the Store
instance, like a document identifier.
- id: some
Hashable
value derived fromEnvironment
that is constant and unique to the store using this reducer. You can use any function, or aKeyPath<Environment, ID>
.
A reducer that defines a namespace for identifiers defined with the @EffectID
property wrapper.
A version of pullback(state:action:environment:)
that transforms a reducer that works on
an element into one that works on an identified array of elements.
public func forEachNamespaced<GlobalState, GlobalAction, GlobalEnvironment, ID>(
state toLocalState: WritableKeyPath<GlobalState, IdentifiedArray<ID, State>>,
action toLocalAction: CasePath<GlobalAction, (ID, Action)>,
environment toLocalEnvironment: @escaping (GlobalEnvironment) -> Environment,
file: StaticString = #fileID,
line: UInt = #line
) -> Reducer<GlobalState, GlobalAction, GlobalEnvironment>
// Global domain that holds a collection of local domains:
struct AppState { var todos: IdentifiedArrayOf<Todo> }
enum AppAction { case todo(id: Todo.ID, action: TodoAction) }
struct AppEnvironment { var mainQueue: AnySchedulerOf<DispatchQueue> }
// A reducer that works on a local domain:
let todoReducer = Reducer<Todo, TodoAction, TodoEnvironment> { ... }
// Pullback the local todo reducer so that it works on all of the app domain:
let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
todoReducer.forEach(
state: \.todos,
action: /AppAction.todo(id:action:),
environment: { _ in TodoEnvironment() }
),
Reducer { state, action, environment in
...
}
)
Take care when combining forEach(state:action:environment:file:line:)-gvte
reducers into
parent domains, as order matters. Always combine
forEach(state:action:environment:file:line:)-gvte
reducers before parent reducers that
can modify the collection.
- toLocalState: A key path that can get/set a collection of
State
elements insideGlobalState
. - toLocalAction: A case path that can extract/embed
(Collection.Index, Action)
fromGlobalAction
. - toLocalEnvironment: A function that transforms
GlobalEnvironment
intoEnvironment
.
A reducer that works on GlobalState
, GlobalAction
, GlobalEnvironment
.
A version of pullback(state:action:environment:)
that transforms a reducer that works on
an element into one namespaced reducer that works on a dictionary of element values.
public func forEachNamespaced<GlobalState, GlobalAction, GlobalEnvironment, Key>(
state toLocalState: WritableKeyPath<GlobalState, [Key: State]>,
action toLocalAction: CasePath<GlobalAction, (Key, Action)>,
environment toLocalEnvironment: @escaping (GlobalEnvironment) -> Environment,
file: StaticString = #fileID,
line: UInt = #line
) -> Reducer<GlobalState, GlobalAction, GlobalEnvironment>
The dictionary's key is used to namespace the element's reducer.
Take care when combining forEachNamespaced(state:action:environment:file:line:)
reducers into parent domains, as order matters. Always combine
forEachNamespaced(state:action:environment:file:line:)
reducers before parent reducers
that can modify the dictionary.
- toLocalState: A key path that can get/set a dictionary of
State
values insideGlobalState
. - toLocalAction: A case path that can extract/embed
(Key, Action)
fromGlobalAction
. - toLocalEnvironment: A function that transforms
GlobalEnvironment
intoEnvironment
.
A reducer that works on GlobalState
, GlobalAction
, GlobalEnvironment
.
A version of pullback(state:action:environment:)
that transforms a reducer that works on
an element into one namespaced reducer that works on an identified array of elements wrapped
in Identified
s.
public func forEachNamespaced<GlobalState, GlobalAction, GlobalEnvironment, ID>(
state toLocalState: WritableKeyPath<
GlobalState, IdentifiedArrayOf<Identified<ID, State>>
>,
action toLocalAction: CasePath<GlobalAction, (ID, Action)>,
environment toLocalEnvironment: @escaping (GlobalEnvironment) -> Environment,
file: StaticString = #fileID,
line: UInt = #line
) -> Reducer<GlobalState, GlobalAction, GlobalEnvironment>
The wrapper's Identified/id
is used to namespace the element's reducer.
// Global domain that holds a collection of local domains:
struct AppState { var todos: IdentifiedArrayOf<Todo> }
enum AppAction { case todo(id: Todo.ID, action: TodoAction) }
struct AppEnvironment { var mainQueue: AnySchedulerOf<DispatchQueue> }
// A reducer that works on a local domain:
let todoReducer = Reducer<Todo, TodoAction, TodoEnvironment> { ... }
// Pullback the local todo reducer so that it works on all of the app domain:
let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
todoReducer.forEach(
state: \.todos,
action: /AppAction.todo(id:action:),
environment: { _ in TodoEnvironment() }
),
Reducer { state, action, environment in
...
}
)
Take care when combining forEach(state:action:environment:file:line:)-gvte
reducers into
parent domains, as order matters. Always combine
forEach(state:action:environment:file:line:)-gvte
reducers before parent reducers that
can modify the collection.
- toLocalState: A key path that can get/set a collection of
State
elements insideGlobalState
. - toLocalAction: A case path that can extract/embed
(Collection.Index, Action)
fromGlobalAction
. - toLocalEnvironment: A function that transforms
GlobalEnvironment
intoEnvironment
.
A reducer that works on GlobalState
, GlobalAction
, GlobalEnvironment
.
Generated at 2022-06-03T18:53:26+0000 using swift-doc 1.0.0-rc.1.