Skip to content

Commit 6d9bd6e

Browse files
mbrandonwp4checo
authored andcommitted
More docs on transformDependency and a fusion fix. (#1495)
* More docs on transformDependency and a fusion fix. * wip (cherry picked from commit 4a4d9cd119ed0eeda650acbde5a4d2a3741a003c)
1 parent fbb68d9 commit 6d9bd6e

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Sources/ComposableArchitecture/Reducer/Reducers/DependencyKeyWritingReducer.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension ReducerProtocol {
8484
/// value.
8585
///
8686
/// For example, suppose you want to see when a particular endpoint of a dependency gets called
87-
/// in your application. You can override that endpoint to insert a breakpoint or print state,
87+
/// in your application. You can override that endpoint to insert a breakpoint or print statement,
8888
/// but still call out to the original endpoint:
8989
///
9090
/// ```swift
@@ -97,6 +97,20 @@ extension ReducerProtocol {
9797
/// }
9898
/// ```
9999
///
100+
/// You can also transform _all_ dependency values at once by using the `\.self` key path:
101+
///
102+
/// ```swift
103+
/// Feature()
104+
/// .transformDependency(\.self) { dependencyValues in
105+
/// // Access to all dependencies in here
106+
/// }
107+
/// ```
108+
///
109+
/// > Warning: The trailing closure of ``transformDependency(_:transform:)`` is called for every
110+
/// action sent to the reducer, and so you can expect it to be called many times in an
111+
/// application's lifecycle. This means you should typically not create dependencies in the
112+
/// closure as that will cause a new dependency to be created everytime an action is sent.
113+
///
100114
/// - Parameters:
101115
/// - keyPath: A key path that indicates the property of the `DependencyValues` structure to
102116
/// transform.
@@ -155,6 +169,9 @@ public struct _DependencyKeyWritingReducer<Base: ReducerProtocol>: ReducerProtoc
155169
_ keyPath: WritableKeyPath<DependencyValues, V>,
156170
transform: @escaping (inout V) -> Void
157171
) -> Self {
158-
Self(base: self.base) { transform(&$0[keyPath: keyPath]) }
172+
Self(base: self.base) { values in
173+
transform(&values[keyPath: keyPath])
174+
self.update(&values)
175+
}
159176
}
160177
}

Tests/ComposableArchitectureTests/DependencyKeyWritingReducerTests.swift

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ final class DependencyKeyWritingReducerTests: XCTestCase {
66
func testWritingFusion() async {
77
let reducer: _DependencyKeyWritingReducer<Feature> = Feature()
88
.dependency(\.myValue, 42)
9-
let _: _DependencyKeyWritingReducer<Feature> =
10-
reducer
119
.dependency(\.myValue, 1729)
1210
.dependency(\.myValue, 1)
1311
.dependency(\.myValue, 2)
1412
.dependency(\.myValue, 3)
13+
14+
XCTAssertTrue((reducer as Any) is _DependencyKeyWritingReducer<Feature>)
15+
}
16+
17+
func testTransformFusion() async {
18+
let reducer: _DependencyKeyWritingReducer<Feature> = Feature()
19+
.transformDependency(\.myValue) { $0 = 42 }
20+
.transformDependency(\.myValue) { $0 = 1729 }
21+
.transformDependency(\.myValue) { $0 = 1 }
22+
.transformDependency(\.myValue) { $0 = 2 }
23+
.transformDependency(\.myValue) { $0 = 3 }
24+
25+
XCTAssertTrue((reducer as Any) is _DependencyKeyWritingReducer<Feature>)
1526
}
1627

1728
func testWritingFusionOrder() async {
@@ -29,12 +40,44 @@ final class DependencyKeyWritingReducerTests: XCTestCase {
2940
}
3041
}
3142

43+
func testTransformFusionOrder() async {
44+
let reducer = Feature()
45+
.transformDependency(\.myValue) { $0 = 42 }
46+
.transformDependency(\.myValue) { $0 = 1729 }
47+
48+
let store = TestStore(
49+
initialState: Feature.State(),
50+
reducer: reducer
51+
)
52+
53+
await store.send(.tap) {
54+
$0.value = 42
55+
}
56+
}
57+
3258
func testWritingOrder() async {
3359
let reducer = CombineReducers {
3460
Feature()
3561
.dependency(\.myValue, 42)
3662
}
37-
.dependency(\.myValue, 1729)
63+
.dependency(\.myValue, 1729)
64+
65+
let store = TestStore(
66+
initialState: Feature.State(),
67+
reducer: reducer
68+
)
69+
70+
await store.send(.tap) {
71+
$0.value = 42
72+
}
73+
}
74+
75+
func testTransformOrder() async {
76+
let reducer = CombineReducers {
77+
Feature()
78+
.transformDependency(\.myValue) { $0 = 42 }
79+
}
80+
.transformDependency(\.myValue) { $0 = 1729 }
3881

3982
let store = TestStore(
4083
initialState: Feature.State(),

0 commit comments

Comments
 (0)