-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestScheduler.swift
111 lines (93 loc) · 3.28 KB
/
TestScheduler.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
//
// TestScheduler.swift
//
//
// Created by Gleb Radchenko on 16.01.21.
//
import Combine
import Foundation
import SchedulerKit
open class TestScheduler: Scheduler {
public typealias SchedulerTimeType = DispatchQueue.SchedulerTimeType
public typealias SchedulerOptions = DispatchQueue.SchedulerOptions
public init() {}
/// Scheduler needs to be advanced before execution for delayed actions with interval
public var enableImmediateExecutionIfPossible = true
public func advance(by interval: DispatchQueue.SchedulerTimeType.Stride) {
_now = _now.advanced(by: interval)
}
public var _now: DispatchQueue.SchedulerTimeType = .init(.now())
public var now: DispatchQueue.SchedulerTimeType { _now }
public var _minimumTolerance: DispatchQueue.SchedulerTimeType.Stride = .zero
public var minimumTolerance: DispatchQueue.SchedulerTimeType.Stride { _minimumTolerance }
public var immediateActions: [ImmediateAction] = []
public func schedule(options: DispatchQueue.SchedulerOptions?, _ action: @escaping () -> Void) {
immediateActions.append(
ImmediateAction(options: options, action: action)
)
if enableImmediateExecutionIfPossible {
action()
}
}
public var delayedActions: [DelayedAction] = []
public func schedule(
after: SchedulerTimeType,
tolerance: SchedulerTimeType.Stride,
options: SchedulerOptions?,
_ action: @escaping () -> Void
) {
delayedActions.append(
DelayedAction(
after: after,
tolerance: tolerance,
options: options,
action: action
)
)
if enableImmediateExecutionIfPossible {
action()
}
}
public var delayedIntervalActions: [DelayedIntervalAction] = []
public func schedule(
after: SchedulerTimeType,
interval: SchedulerTimeType.Stride,
tolerance: SchedulerTimeType.Stride,
options : SchedulerOptions?,
_ action: @escaping () -> Void
) -> Cancellable {
let delayedIntervalAction = DelayedIntervalAction(
after: after,
interval: interval,
tolerance: tolerance,
options: options,
action: action
)
delayedIntervalActions.append(delayedIntervalAction)
return AnyCancellable { [weak self] in
self?.delayedIntervalActions.removeAll(where: { $0.uuid == delayedIntervalAction.uuid })
}
}
}
public extension TestScheduler {
struct ImmediateAction {
fileprivate let uuid = UUID()
public let options: DispatchQueue.SchedulerOptions?
public let action: () -> Void
}
struct DelayedAction {
fileprivate let uuid = UUID()
public let after: SchedulerTimeType
public let tolerance: SchedulerTimeType.Stride
public let options: SchedulerOptions?
public let action: () -> Void
}
struct DelayedIntervalAction {
fileprivate let uuid = UUID()
public let after: SchedulerTimeType
public let interval: SchedulerTimeType.Stride
public let tolerance: SchedulerTimeType.Stride
public let options: SchedulerOptions?
public let action: () -> Void
}
}