Skip to content

Commit

Permalink
Add Reducer.dependency(value) (#2863)
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
stephencelis authored Feb 26, 2024
1 parent 434c648 commit b8b726c
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ extension Reducer {
_DependencyKeyWritingReducer(base: self) { $0[keyPath: keyPath] = value }
}

/// Places a value in the reducer's dependencies.
///
/// - Parameter value: The value to set for this value's type in the dependencies.
/// - Returns: A reducer that has the given value set in its dependencies.
@inlinable
@warn_unqualified_access
public func dependency<Value: TestDependencyKey>(
_ value: Value
)
// NB: We should not return `some Reducer<State, Action>` here. That would prevent the
// specialization defined below from being called, which fuses chained calls.
-> _DependencyKeyWritingReducer<Self>
where Value.Value == Value {
_DependencyKeyWritingReducer(base: self) { $0[Value.self] = value }
}

/// Transform a reducer's dependency value at the specified key path with the given function.
///
/// This is similar to ``dependency(_:_:)``, except it allows you to mutate a dependency value
Expand Down

0 comments on commit b8b726c

Please sign in to comment.