Skip to content

Commit b8b726c

Browse files
authored
Add Reducer.dependency(value) (#2863)
This new reducer operator can be used to directly assign a dependency key value to the reducer's dependencies. ```swift // Before; @dependency(\.apiClient) var apiClient // ... .dependency(\.apiClient, apiClient) // After: @dependency(APIClient.self) var apiClient // ... .dependency(apiClient) ```
1 parent 434c648 commit b8b726c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/ComposableArchitecture/Reducer/Reducers/DependencyKeyWritingReducer.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ extension Reducer {
7979
_DependencyKeyWritingReducer(base: self) { $0[keyPath: keyPath] = value }
8080
}
8181

82+
/// Places a value in the reducer's dependencies.
83+
///
84+
/// - Parameter value: The value to set for this value's type in the dependencies.
85+
/// - Returns: A reducer that has the given value set in its dependencies.
86+
@inlinable
87+
@warn_unqualified_access
88+
public func dependency<Value: TestDependencyKey>(
89+
_ value: Value
90+
)
91+
// NB: We should not return `some Reducer<State, Action>` here. That would prevent the
92+
// specialization defined below from being called, which fuses chained calls.
93+
-> _DependencyKeyWritingReducer<Self>
94+
where Value.Value == Value {
95+
_DependencyKeyWritingReducer(base: self) { $0[Value.self] = value }
96+
}
97+
8298
/// Transform a reducer's dependency value at the specified key path with the given function.
8399
///
84100
/// This is similar to ``dependency(_:_:)``, except it allows you to mutate a dependency value

0 commit comments

Comments
 (0)