|
1 | 1 | #if canImport(SwiftUI) |
2 | 2 | import ReactiveSwift |
3 | | - import SwiftUI |
| 3 | +import SwiftUI |
4 | 4 |
|
5 | 5 | extension EffectProducer { |
6 | | - /// Wraps the emission of each element with SwiftUI's `withAnimation`. |
7 | | - /// |
8 | | - /// ```swift |
9 | | - /// case .buttonTapped: |
10 | | - /// return .task { |
11 | | - /// .activityResponse(await self.apiClient.fetchActivity()) |
12 | | - /// } |
13 | | - /// .animation() |
14 | | - /// ``` |
15 | | - /// |
16 | | - /// - Parameter animation: An animation. |
| 6 | + /// Wraps the emission of each element with SwiftUI's `withAnimation`. |
| 7 | + /// |
| 8 | + /// ```swift |
| 9 | + /// case .buttonTapped: |
| 10 | + /// return .task { |
| 11 | + /// .activityResponse(await self.apiClient.fetchActivity()) |
| 12 | + /// } |
| 13 | + /// .animation() |
| 14 | + /// ``` |
| 15 | + /// |
| 16 | + /// - Parameter animation: An animation. |
17 | 17 | /// - Returns: An effect. |
18 | | - public func animation(_ animation: Animation? = .default) -> Self { |
19 | | - switch self.operation { |
20 | | - case .none: |
21 | | - return .none |
| 18 | + public func animation(_ animation: Animation? = .default) -> Self { |
| 19 | + self.transaction(Transaction(animation: animation)) |
| 20 | + } |
| 21 | + |
| 22 | + /// Wraps the emission of each element with SwiftUI's `withTransaction`. |
| 23 | + /// |
| 24 | + /// ```swift |
| 25 | + /// case .buttonTapped: |
| 26 | + /// var transaction = Transaction(animation: .default) |
| 27 | + /// transaction.disablesAnimations = true |
| 28 | + /// return .task { |
| 29 | + /// .activityResponse(await self.apiClient.fetchActivity()) |
| 30 | + /// } |
| 31 | + /// .transaction(transaction) |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + /// - Parameter transaction: A transaction. |
| 35 | + /// - Returns: A publisher. |
| 36 | + public func transaction(_ transaction: Transaction) -> Self { |
| 37 | + switch self.operation { |
| 38 | + case .none: |
| 39 | + return .none |
22 | 40 | case let .producer(producer): |
23 | | - return Self( |
| 41 | + return Self( |
24 | 42 | operation: .producer( |
25 | 43 | SignalProducer<Action, Failure> { observer, _ in |
26 | 44 | producer.start { action in |
27 | 45 | switch action { |
28 | 46 | case let .value(value): |
29 | | - withAnimation(animation) { |
| 47 | + withTransaction(transaction) { |
30 | 48 | observer.send(value: value) |
31 | 49 | } |
32 | 50 | case .completed: |
|
38 | 56 | } |
39 | 57 | } |
40 | 58 | } |
41 | | - ) |
42 | 59 | ) |
43 | | - case let .run(priority, operation): |
44 | | - return Self( |
45 | | - operation: .run(priority) { send in |
46 | | - await operation( |
47 | | - Send { value in |
48 | | - withAnimation(animation) { |
49 | | - send(value) |
50 | | - } |
| 60 | + ) |
| 61 | + case let .run(priority, operation): |
| 62 | + return Self( |
| 63 | + operation: .run(priority) { send in |
| 64 | + await operation( |
| 65 | + Send { value in |
| 66 | + withTransaction(transaction) { |
| 67 | + send(value) |
51 | 68 | } |
52 | | - ) |
53 | | - } |
54 | | - ) |
55 | | - } |
| 69 | + } |
| 70 | + ) |
| 71 | + } |
| 72 | + ) |
56 | 73 | } |
57 | 74 | } |
| 75 | +} |
58 | 76 | #endif |
0 commit comments