-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Effect.swift
384 lines (360 loc) · 12 KB
/
Effect.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import Combine
import Foundation
import SwiftUI
import XCTestDynamicOverlay
public struct Effect<Action> {
@usableFromInline
enum Operation {
case none
case publisher(AnyPublisher<Action, Never>)
case run(TaskPriority? = nil, @Sendable (_ send: Send<Action>) async -> Void)
}
@usableFromInline
let operation: Operation
@usableFromInline
init(operation: Operation) {
self.operation = operation
}
}
/// A convenience type alias for referring to an effect of a given reducer's domain.
///
/// Instead of specifying the action:
///
/// ```swift
/// let effect: Effect<Feature.Action>
/// ```
///
/// You can specify the reducer:
///
/// ```swift
/// let effect: EffectOf<Feature>
/// ```
public typealias EffectOf<R: Reducer> = Effect<R.Action>
// MARK: - Creating Effects
extension Effect {
/// An effect that does nothing and completes immediately. Useful for situations where you must
/// return an effect, but you don't need to do anything.
@inlinable
public static var none: Self {
Self(operation: .none)
}
/// Wraps an asynchronous unit of work that can emit actions any number of times in an effect.
///
/// For example, if you had an async stream in a dependency client:
///
/// ```swift
/// struct EventsClient {
/// var events: () -> AsyncStream<Event>
/// }
/// ```
///
/// Then you could attach to it in a `run` effect by using `for await` and sending each action of
/// the stream back into the system:
///
/// ```swift
/// case .startButtonTapped:
/// return .run { send in
/// for await event in self.events() {
/// send(.event(event))
/// }
/// }
/// ```
///
/// See ``Send`` for more information on how to use the `send` argument passed to `run`'s closure.
///
/// The closure provided to ``run(priority:operation:catch:fileID:line:)`` is allowed to
/// throw, but any non-cancellation errors thrown will cause a runtime warning when run in the
/// simulator or on a device, and will cause a test failure in tests. To catch non-cancellation
/// errors use the `catch` trailing closure.
///
/// - Parameters:
/// - priority: Priority of the underlying task. If `nil`, the priority will come from
/// `Task.currentPriority`.
/// - operation: The operation to execute.
/// - catch: An error handler, invoked if the operation throws an error other than
/// `CancellationError`.
/// - Returns: An effect wrapping the given asynchronous work.
public static func run(
priority: TaskPriority? = nil,
operation: @escaping @Sendable (_ send: Send<Action>) async throws -> Void,
catch handler: (@Sendable (_ error: Error, _ send: Send<Action>) async -> Void)? = nil,
fileID: StaticString = #fileID,
line: UInt = #line
) -> Self {
withEscapedDependencies { escaped in
Self(
operation: .run(priority) { send in
await escaped.yield {
do {
try await operation(send)
} catch is CancellationError {
return
} catch {
guard let handler = handler else {
#if DEBUG
var errorDump = ""
customDump(error, to: &errorDump, indent: 4)
runtimeWarn(
"""
An "Effect.run" returned from "\(fileID):\(line)" threw an unhandled error. …
\(errorDump)
All non-cancellation errors must be explicitly handled via the "catch" parameter \
on "Effect.run", or via a "do" block.
"""
)
#endif
return
}
await handler(error, send)
}
}
}
)
}
}
/// Initializes an effect that immediately emits the action passed in.
///
/// > Note: We do not recommend using `Effect.send` to share logic. Instead, limit usage to
/// > child-parent communication, where a child may want to emit a "delegate" action for a parent
/// > to listen to.
/// >
/// > For more information, see <doc:Performance#Sharing-logic-with-actions>.
///
/// - Parameter action: The action that is immediately emitted by the effect.
public static func send(_ action: Action) -> Self {
Self(operation: .publisher(Just(action).eraseToAnyPublisher()))
}
/// Initializes an effect that immediately emits the action passed in.
///
/// > Note: We do not recommend using `Effect.send` to share logic. Instead, limit usage to
/// > child-parent communication, where a child may want to emit a "delegate" action for a parent
/// > to listen to.
/// >
/// > For more information, see <doc:Performance#Sharing-logic-with-actions>.
///
/// - Parameters:
/// - action: The action that is immediately emitted by the effect.
/// - animation: An animation.
public static func send(_ action: Action, animation: Animation? = nil) -> Self {
.send(action).animation(animation)
}
}
/// A type that can send actions back into the system when used from
/// ``Effect/run(priority:operation:catch:fileID:line:)``.
///
/// This type implements [`callAsFunction`][callAsFunction] so that you invoke it as a function
/// rather than calling methods on it:
///
/// ```swift
/// return .run { send in
/// send(.started)
/// defer { send(.finished) }
/// for await event in self.events {
/// send(.event(event))
/// }
/// }
/// ```
///
/// You can also send actions with animation:
///
/// ```swift
/// send(.started, animation: .spring())
/// defer { send(.finished, animation: .default) }
/// ```
///
/// See ``Effect/run(priority:operation:catch:fileID:line:)`` for more information on how to
/// use this value to construct effects that can emit any number of times in an asynchronous
/// context.
///
/// [callAsFunction]: https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID622
@MainActor
public struct Send<Action>: Sendable {
let send: @MainActor @Sendable (Action) -> Void
public init(send: @escaping @MainActor @Sendable (Action) -> Void) {
self.send = send
}
/// Sends an action back into the system from an effect.
///
/// - Parameter action: An action.
public func callAsFunction(_ action: Action) {
guard !Task.isCancelled else { return }
self.send(action)
}
/// Sends an action back into the system from an effect with animation.
///
/// - Parameters:
/// - action: An action.
/// - animation: An animation.
public func callAsFunction(_ action: Action, animation: Animation?) {
callAsFunction(action, transaction: Transaction(animation: animation))
}
/// Sends an action back into the system from an effect with transaction.
///
/// - Parameters:
/// - action: An action.
/// - transaction: A transaction.
public func callAsFunction(_ action: Action, transaction: Transaction) {
guard !Task.isCancelled else { return }
withTransaction(transaction) {
self(action)
}
}
}
// MARK: - Composing Effects
extension Effect {
/// Merges a variadic list of effects together into a single effect, which runs the effects at the
/// same time.
///
/// - Parameter effects: A variadic list of effects.
/// - Returns: A new effect
@inlinable
public static func merge(_ effects: Self...) -> Self {
Self.merge(effects)
}
/// Merges a sequence of effects together into a single effect, which runs the effects at the same
/// time.
///
/// - Parameter effects: A sequence of effects.
/// - Returns: A new effect
@inlinable
public static func merge<S: Sequence>(_ effects: S) -> Self where S.Element == Self {
effects.reduce(.none) { $0.merge(with: $1) }
}
/// Merges this effect and another into a single effect that runs both at the same time.
///
/// - Parameter other: Another effect.
/// - Returns: An effect that runs this effect and the other at the same time.
@inlinable
public func merge(with other: Self) -> Self {
switch (self.operation, other.operation) {
case (_, .none):
return self
case (.none, _):
return other
case (.publisher, .publisher), (.run, .publisher), (.publisher, .run):
return Self(
operation: .publisher(
Publishers.Merge(
_EffectPublisher(self),
_EffectPublisher(other)
)
.eraseToAnyPublisher()
)
)
case let (.run(lhsPriority, lhsOperation), .run(rhsPriority, rhsOperation)):
return Self(
operation: .run { send in
await withTaskGroup(of: Void.self) { group in
group.addTask(priority: lhsPriority) {
await lhsOperation(send)
}
group.addTask(priority: rhsPriority) {
await rhsOperation(send)
}
}
}
)
}
}
/// Concatenates a variadic list of effects together into a single effect, which runs the effects
/// one after the other.
///
/// - Parameter effects: A variadic list of effects.
/// - Returns: A new effect
@inlinable
public static func concatenate(_ effects: Self...) -> Self {
Self.concatenate(effects)
}
/// Concatenates a collection of effects together into a single effect, which runs the effects one
/// after the other.
///
/// - Parameter effects: A collection of effects.
/// - Returns: A new effect
@inlinable
public static func concatenate<C: Collection>(_ effects: C) -> Self where C.Element == Self {
effects.reduce(.none) { $0.concatenate(with: $1) }
}
/// Concatenates this effect and another into a single effect that first runs this effect, and
/// after it completes or is cancelled, runs the other.
///
/// - Parameter other: Another effect.
/// - Returns: An effect that runs this effect, and after it completes or is cancelled, runs the
/// other.
@inlinable
@_disfavoredOverload
public func concatenate(with other: Self) -> Self {
switch (self.operation, other.operation) {
case (_, .none):
return self
case (.none, _):
return other
case (.publisher, .publisher), (.run, .publisher), (.publisher, .run):
return Self(
operation: .publisher(
Publishers.Concatenate(
prefix: _EffectPublisher(self),
suffix: _EffectPublisher(other)
)
.eraseToAnyPublisher()
)
)
case let (.run(lhsPriority, lhsOperation), .run(rhsPriority, rhsOperation)):
return Self(
operation: .run { send in
if let lhsPriority = lhsPriority {
await Task(priority: lhsPriority) { await lhsOperation(send) }.cancellableValue
} else {
await lhsOperation(send)
}
if let rhsPriority = rhsPriority {
await Task(priority: rhsPriority) { await rhsOperation(send) }.cancellableValue
} else {
await rhsOperation(send)
}
}
)
}
}
/// Transforms all elements from the upstream effect with a provided closure.
///
/// - Parameter transform: A closure that transforms the upstream effect's action to a new action.
/// - Returns: A publisher that uses the provided closure to map elements from the upstream effect
/// to new elements that it then publishes.
@inlinable
public func map<T>(_ transform: @escaping (Action) -> T) -> Effect<T> {
switch self.operation {
case .none:
return .none
case let .publisher(publisher):
return .init(
operation: .publisher(
publisher
.map(
withEscapedDependencies { escaped in
{ action in
escaped.yield {
transform(action)
}
}
}
)
.eraseToAnyPublisher()
)
)
case let .run(priority, operation):
return withEscapedDependencies { escaped in
.init(
operation: .run(priority) { send in
await escaped.yield {
await operation(
Send { action in
send(transform(action))
}
)
}
}
)
}
}
}
}