|
1 |
| -import Foundation |
| 1 | + import Foundation |
2 | 2 | import ReactiveSwift
|
3 | 3 | import XCTestDynamicOverlay
|
4 | 4 |
|
5 |
| -extension DependencyValues { |
6 |
| - /// The "main" queue. |
7 |
| - /// |
8 |
| - /// Introduce controllable timing to your features by using the ``Dependency`` property wrapper |
| 5 | + extension DependencyValues { |
| 6 | + /// The "main" queue. |
| 7 | + /// |
| 8 | + /// Introduce controllable timing to your features by using the ``Dependency`` property wrapper |
9 | 9 | /// with a key path to this property. The wrapped value is a ReactiveSwift scheduler with the time
|
10 |
| - /// type and options of a dispatch queue. By default, `DispatchQueue.main` will be provided, |
11 |
| - /// with the exception of XCTest cases, in which an "unimplemented" scheduler will be provided. |
12 |
| - /// |
13 |
| - /// For example, you could introduce controllable timing to a Composable Architecture reducer |
14 |
| - /// that counts the number of seconds it's onscreen: |
15 |
| - /// |
16 |
| - /// ``` |
17 |
| - /// struct TimerReducer: ReducerProtocol { |
18 |
| - /// struct State { |
19 |
| - /// var elapsed = 0 |
20 |
| - /// } |
21 |
| - /// |
22 |
| - /// enum Action { |
23 |
| - /// case task |
24 |
| - /// case timerTicked |
25 |
| - /// } |
26 |
| - /// |
27 |
| - /// @Dependency(\.mainQueue) var mainQueue |
28 |
| - /// |
29 |
| - /// func reduce(into state: inout State, action: Action) -> EffectTask<Action> { |
30 |
| - /// switch action { |
31 |
| - /// case .task: |
32 |
| - /// return .run { send in |
33 |
| - /// for await _ in self.mainQueue.timer(interval: .seconds(1)) { |
34 |
| - /// send(.timerTicked) |
35 |
| - /// } |
36 |
| - /// } |
37 |
| - /// |
38 |
| - /// case .timerTicked: |
39 |
| - /// state.elapsed += 1 |
40 |
| - /// return .none |
41 |
| - /// } |
42 |
| - /// } |
43 |
| - /// } |
44 |
| - /// ``` |
45 |
| - /// |
46 |
| - /// And you could test this reducer by overriding its main queue with a test scheduler: |
47 |
| - /// |
48 |
| - /// ``` |
| 10 | + /// type and options of a dispatch queue. By default, `DispatchQueue.main` will be provided, |
| 11 | + /// with the exception of XCTest cases, in which an "unimplemented" scheduler will be provided. |
| 12 | + /// |
| 13 | + /// For example, you could introduce controllable timing to a Composable Architecture reducer |
| 14 | + /// that counts the number of seconds it's onscreen: |
| 15 | + /// |
| 16 | + /// ``` |
| 17 | + /// struct TimerReducer: ReducerProtocol { |
| 18 | + /// struct State { |
| 19 | + /// var elapsed = 0 |
| 20 | + /// } |
| 21 | + /// |
| 22 | + /// enum Action { |
| 23 | + /// case task |
| 24 | + /// case timerTicked |
| 25 | + /// } |
| 26 | + /// |
| 27 | + /// @Dependency(\.mainQueue) var mainQueue |
| 28 | + /// |
| 29 | + /// func reduce(into state: inout State, action: Action) -> EffectTask<Action> { |
| 30 | + /// switch action { |
| 31 | + /// case .task: |
| 32 | + /// return .run { send in |
| 33 | + /// for await _ in self.mainQueue.timer(interval: .seconds(1)) { |
| 34 | + /// await send(.timerTicked) |
| 35 | + /// } |
| 36 | + /// } |
| 37 | + /// |
| 38 | + /// case .timerTicked: |
| 39 | + /// state.elapsed += 1 |
| 40 | + /// return .none |
| 41 | + /// } |
| 42 | + /// } |
| 43 | + /// } |
| 44 | + /// ``` |
| 45 | + /// |
| 46 | + /// And you could test this reducer by overriding its main queue with a test scheduler: |
| 47 | + /// |
| 48 | + /// ``` |
49 | 49 | /// let mainQueue = TestScheduler()
|
50 |
| - /// |
51 |
| - /// let store = TestStore( |
52 |
| - /// initialState: TimerReducer.State() |
53 |
| - /// reducer: TimerReducer() |
| 50 | + /// |
| 51 | + /// let store = TestStore( |
| 52 | + /// initialState: TimerReducer.State() |
| 53 | + /// reducer: TimerReducer() |
54 | 54 | /// .dependency(\.mainQueue, mainQueue)
|
55 |
| - /// ) |
56 |
| - /// |
57 |
| - /// let task = store.send(.task) |
58 |
| - /// |
59 |
| - /// mainQueue.advance(by: .seconds(1) |
60 |
| - /// await store.receive(.timerTicked) { |
61 |
| - /// $0.elapsed = 1 |
62 |
| - /// } |
63 |
| - /// mainQueue.advance(by: .seconds(1) |
64 |
| - /// await store.receive(.timerTicked) { |
65 |
| - /// $0.elapsed = 2 |
66 |
| - /// } |
67 |
| - /// await task.cancel() |
68 |
| - /// ``` |
69 |
| - @available( |
70 |
| - iOS, deprecated: 9999.0, message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
71 |
| - ) |
72 |
| - @available( |
73 |
| - macOS, deprecated: 9999.0, |
74 |
| - message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
75 |
| - ) |
76 |
| - @available( |
77 |
| - tvOS, |
78 |
| - deprecated: 9999.0, |
79 |
| - message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
80 |
| - ) |
81 |
| - @available( |
82 |
| - watchOS, |
83 |
| - deprecated: 9999.0, |
84 |
| - message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
85 |
| - ) |
| 55 | + /// ) |
| 56 | + /// |
| 57 | + /// let task = store.send(.task) |
| 58 | + /// |
| 59 | + /// mainQueue.advance(by: .seconds(1) |
| 60 | + /// await store.receive(.timerTicked) { |
| 61 | + /// $0.elapsed = 1 |
| 62 | + /// } |
| 63 | + /// mainQueue.advance(by: .seconds(1) |
| 64 | + /// await store.receive(.timerTicked) { |
| 65 | + /// $0.elapsed = 2 |
| 66 | + /// } |
| 67 | + /// await task.cancel() |
| 68 | + /// ``` |
| 69 | + @available( |
| 70 | + iOS, deprecated: 9999.0, message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
| 71 | + ) |
| 72 | + @available( |
| 73 | + macOS, deprecated: 9999.0, |
| 74 | + message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
| 75 | + ) |
| 76 | + @available( |
| 77 | + tvOS, |
| 78 | + deprecated: 9999.0, |
| 79 | + message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
| 80 | + ) |
| 81 | + @available( |
| 82 | + watchOS, |
| 83 | + deprecated: 9999.0, |
| 84 | + message: "Use '\\.continuousClock' or '\\.suspendingClock' instead." |
| 85 | + ) |
86 | 86 | public var mainQueue: DateScheduler {
|
87 |
| - get { self[MainQueueKey.self] } |
88 |
| - set { self[MainQueueKey.self] = newValue } |
89 |
| - } |
| 87 | + get { self[MainQueueKey.self] } |
| 88 | + set { self[MainQueueKey.self] = newValue } |
| 89 | + } |
90 | 90 |
|
91 |
| - private enum MainQueueKey: DependencyKey { |
| 91 | + private enum MainQueueKey: DependencyKey { |
92 | 92 | static let liveValue = QueueScheduler.main as DateScheduler
|
93 | 93 | static let testValue = UnimplementedScheduler() as DateScheduler
|
| 94 | + } |
94 | 95 | }
|
95 |
| -} |
96 | 96 |
|
97 | 97 | public final class UnimplementedScheduler: DateScheduler {
|
98 | 98 |
|
|
0 commit comments