Skip to content

Commit 8e64660

Browse files
committedJul 22, 2021
Add includeFinished flag to forward operator
1 parent 6e0f3cb commit 8e64660

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed
 

‎Sources/RecombinePackage/Helpers/AssignOwnership.swift

+96-21
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,48 @@ public extension Publisher {
5252
func forward<Root: AnyObject, S: Subject>(
5353
to keyPath: KeyPath<Root, S>,
5454
on object: Root,
55-
ownership: ObjectOwnership
55+
ownership: ObjectOwnership,
56+
includeFinished: Bool
5657
) -> AnyCancellable
5758
where S.Output == Output, S.Failure == Failure
5859
{
5960
switch ownership {
6061
case .strong:
6162
return sink {
62-
object[keyPath: keyPath].send(completion: $0)
63+
switch $0 {
64+
case .failure:
65+
object[keyPath: keyPath].send(completion: $0)
66+
case .finished:
67+
if includeFinished {
68+
object[keyPath: keyPath].send(completion: $0)
69+
}
70+
}
6371
} receiveValue: {
6472
object[keyPath: keyPath].send($0)
6573
}
6674
case .weak:
6775
return sink { [weak object] in
68-
object?[keyPath: keyPath].send(completion: $0)
76+
switch $0 {
77+
case .failure:
78+
object?[keyPath: keyPath].send(completion: $0)
79+
case .finished:
80+
if includeFinished {
81+
object?[keyPath: keyPath].send(completion: $0)
82+
}
83+
}
6984
} receiveValue: { [weak object] in
7085
object?[keyPath: keyPath].send($0)
7186
}
7287
case .unowned:
7388
return sink { [unowned object] in
74-
object[keyPath: keyPath].send(completion: $0)
89+
switch $0 {
90+
case .failure:
91+
object[keyPath: keyPath].send(completion: $0)
92+
case .finished:
93+
if includeFinished {
94+
object[keyPath: keyPath].send(completion: $0)
95+
}
96+
}
7597
} receiveValue: {
7698
object[keyPath: keyPath].send($0)
7799
}
@@ -81,32 +103,57 @@ public extension Publisher {
81103
func forward<Root1: AnyObject, Root2: AnyObject, S1: Subject, S2: Subject>(
82104
to keyPath1: KeyPath<Root1, S1>, on object1: Root1,
83105
and keyPath2: KeyPath<Root2, S2>, on object2: Root2,
84-
ownership: ObjectOwnership
106+
ownership: ObjectOwnership,
107+
includeFinished: Bool
85108
) -> AnyCancellable
86109
where S1.Output == Output, S1.Failure == Failure,
87110
S2.Output == Output, S2.Failure == Failure
88111
{
89112
switch ownership {
90113
case .strong:
91114
return sink {
92-
object1[keyPath: keyPath1].send(completion: $0)
93-
object2[keyPath: keyPath2].send(completion: $0)
115+
switch $0 {
116+
case .failure:
117+
object1[keyPath: keyPath1].send(completion: $0)
118+
object2[keyPath: keyPath2].send(completion: $0)
119+
case .finished:
120+
if includeFinished {
121+
object1[keyPath: keyPath1].send(completion: $0)
122+
object2[keyPath: keyPath2].send(completion: $0)
123+
}
124+
}
94125
} receiveValue: {
95126
object1[keyPath: keyPath1].send($0)
96127
object2[keyPath: keyPath2].send($0)
97128
}
98129
case .weak:
99130
return sink { [weak object1, weak object2] in
100-
object1?[keyPath: keyPath1].send(completion: $0)
101-
object2?[keyPath: keyPath2].send(completion: $0)
131+
switch $0 {
132+
case .failure:
133+
object1?[keyPath: keyPath1].send(completion: $0)
134+
object2?[keyPath: keyPath2].send(completion: $0)
135+
case .finished:
136+
if includeFinished {
137+
object1?[keyPath: keyPath1].send(completion: $0)
138+
object2?[keyPath: keyPath2].send(completion: $0)
139+
}
140+
}
102141
} receiveValue: { [weak object1, weak object2] in
103142
object1?[keyPath: keyPath1].send($0)
104143
object2?[keyPath: keyPath2].send($0)
105144
}
106145
case .unowned:
107146
return sink { [unowned object1, unowned object2] in
108-
object1[keyPath: keyPath1].send(completion: $0)
109-
object2[keyPath: keyPath2].send(completion: $0)
147+
switch $0 {
148+
case .failure:
149+
object1[keyPath: keyPath1].send(completion: $0)
150+
object2[keyPath: keyPath2].send(completion: $0)
151+
case .finished:
152+
if includeFinished {
153+
object1[keyPath: keyPath1].send(completion: $0)
154+
object2[keyPath: keyPath2].send(completion: $0)
155+
}
156+
}
110157
} receiveValue: { [unowned object1, unowned object2] in
111158
object1[keyPath: keyPath1].send($0)
112159
object2[keyPath: keyPath2].send($0)
@@ -118,7 +165,8 @@ public extension Publisher {
118165
to keyPath1: KeyPath<Root1, S1>, on object1: Root1,
119166
and keyPath2: KeyPath<Root2, S2>, on object2: Root2,
120167
and keyPath3: KeyPath<Root3, S3>, on object3: Root3,
121-
ownership: ObjectOwnership
168+
ownership: ObjectOwnership,
169+
includeFinished: Bool
122170
) -> AnyCancellable
123171
where S1.Output == Output, S1.Failure == Failure,
124172
S2.Output == Output, S2.Failure == Failure,
@@ -127,29 +175,56 @@ public extension Publisher {
127175
switch ownership {
128176
case .strong:
129177
return sink {
130-
object1[keyPath: keyPath1].send(completion: $0)
131-
object2[keyPath: keyPath2].send(completion: $0)
132-
object3[keyPath: keyPath3].send(completion: $0)
178+
switch $0 {
179+
case .failure:
180+
object1[keyPath: keyPath1].send(completion: $0)
181+
object2[keyPath: keyPath2].send(completion: $0)
182+
object3[keyPath: keyPath3].send(completion: $0)
183+
case .finished:
184+
if includeFinished {
185+
object1[keyPath: keyPath1].send(completion: $0)
186+
object2[keyPath: keyPath2].send(completion: $0)
187+
object3[keyPath: keyPath3].send(completion: $0)
188+
}
189+
}
133190
} receiveValue: {
134191
object1[keyPath: keyPath1].send($0)
135192
object2[keyPath: keyPath2].send($0)
136193
object3[keyPath: keyPath3].send($0)
137194
}
138195
case .weak:
139196
return sink { [weak object1, weak object2, weak object3] in
140-
object1?[keyPath: keyPath1].send(completion: $0)
141-
object2?[keyPath: keyPath2].send(completion: $0)
142-
object3?[keyPath: keyPath3].send(completion: $0)
197+
switch $0 {
198+
case .failure:
199+
object1?[keyPath: keyPath1].send(completion: $0)
200+
object2?[keyPath: keyPath2].send(completion: $0)
201+
object3?[keyPath: keyPath3].send(completion: $0)
202+
case .finished:
203+
if includeFinished {
204+
object1?[keyPath: keyPath1].send(completion: $0)
205+
object2?[keyPath: keyPath2].send(completion: $0)
206+
object3?[keyPath: keyPath3].send(completion: $0)
207+
}
208+
}
143209
} receiveValue: { [weak object1, weak object2, weak object3] in
144210
object1?[keyPath: keyPath1].send($0)
145211
object2?[keyPath: keyPath2].send($0)
146212
object3?[keyPath: keyPath3].send($0)
147213
}
148214
case .unowned:
149215
return sink { [unowned object1, unowned object2, unowned object3] in
150-
object1[keyPath: keyPath1].send(completion: $0)
151-
object2[keyPath: keyPath2].send(completion: $0)
152-
object3[keyPath: keyPath3].send(completion: $0)
216+
switch $0 {
217+
case .failure:
218+
object1[keyPath: keyPath1].send(completion: $0)
219+
object2[keyPath: keyPath2].send(completion: $0)
220+
object3[keyPath: keyPath3].send(completion: $0)
221+
case .finished:
222+
if includeFinished {
223+
object1[keyPath: keyPath1].send(completion: $0)
224+
object2[keyPath: keyPath2].send(completion: $0)
225+
object3[keyPath: keyPath3].send(completion: $0)
226+
}
227+
}
153228
} receiveValue: { [unowned object1, unowned object2] in
154229
object1[keyPath: keyPath1].send($0)
155230
object2[keyPath: keyPath2].send($0)

‎Sources/RecombinePackage/Store/BaseStore.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
4242
.forward(
4343
to: \._actionsPairedWithState,
4444
on: self,
45-
ownership: .weak
45+
ownership: .weak,
46+
includeFinished: true
4647
)
4748
.store(in: &cancellables)
4849

@@ -62,7 +63,8 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
6263
.forward(
6364
to: \._postMiddlewareRefinedActions,
6465
on: self,
65-
ownership: .weak
66+
ownership: .weak,
67+
includeFinished: true
6668
)
6769
.store(in: &cancellables)
6870

0 commit comments

Comments
 (0)
Please sign in to comment.