-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathMainQueue.swift
64 lines (62 loc) · 2.04 KB
/
MainQueue.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
#if canImport(Combine)
import Foundation
extension DependencyValues {
/// The "main" queue.
///
/// Introduce controllable timing to your features by using the ``Dependency`` property wrapper
/// with a key path to this property. The wrapped value is a Combine scheduler with the time
/// type and options of a dispatch queue. By default, `DispatchQueue.main` will be provided,
/// with the exception of XCTest cases, in which an "unimplemented" scheduler will be provided.
///
/// For example, you could introduce controllable timing to an observable object model that
/// counts the number of seconds it's onscreen:
///
/// ```swift
/// @Observable
/// final class TimerModel {
/// var elapsed = 0
///
/// @ObservationIgnored
/// @Dependency(\.mainQueue) var mainQueue
///
/// @MainActor
/// func onAppear() async {
/// for await _ in mainQueue.timer(interval: .seconds(1)) {
/// elapsed += 1
/// }
/// }
/// }
/// ```
///
/// And you could test this model by overriding its main queue with a test scheduler:
///
/// ```swift
/// @Test
/// func feature() {
/// let mainQueue = DispatchQueue.test
/// let model = withDependencies {
/// $0.mainQueue = mainQueue.eraseToAnyScheduler()
/// } operation: {
/// TimerModel()
/// }
///
/// Task { await model.onAppear() }
///
/// await mainQueue.advance(by: .seconds(1))
/// XCTAssertEqual(model.elapsed, 1)
///
/// await mainQueue.advance(by: .seconds(4))
/// XCTAssertEqual(model.elapsed, 5)
/// }
/// ```
public var mainQueue: AnySchedulerOf<DispatchQueue> {
get { self[MainQueueKey.self] }
set { self[MainQueueKey.self] = newValue }
}
private enum MainQueueKey: DependencyKey {
static let liveValue = AnySchedulerOf<DispatchQueue>.main
static let testValue = AnySchedulerOf<DispatchQueue>
.unimplemented(#"@Dependency(\.mainQueue)"#)
}
}
#endif