|
| 1 | +// |
| 2 | +// TestScheduler.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Gleb Radchenko on 16.01.21. |
| 6 | +// |
| 7 | + |
| 8 | +import Combine |
| 9 | +import Foundation |
| 10 | +import SchedulerKit |
| 11 | + |
| 12 | +open class TestScheduler: Scheduler { |
| 13 | + public typealias SchedulerTimeType = DispatchQueue.SchedulerTimeType |
| 14 | + public typealias SchedulerOptions = DispatchQueue.SchedulerOptions |
| 15 | + |
| 16 | + public init() {} |
| 17 | + |
| 18 | + /// For delayed actions with interval scheduler needs to be advanced before action execution |
| 19 | + public var enableImmediateExecutionIfPossible = true |
| 20 | + |
| 21 | + public func advance(by interval: DispatchQueue.SchedulerTimeType.Stride) { |
| 22 | + _now = _now.advanced(by: interval) |
| 23 | + } |
| 24 | + |
| 25 | + public var _now: DispatchQueue.SchedulerTimeType = .init(.now()) |
| 26 | + public var now: DispatchQueue.SchedulerTimeType { _now } |
| 27 | + |
| 28 | + public var _minimumTolerance: DispatchQueue.SchedulerTimeType.Stride = .zero |
| 29 | + public var minimumTolerance: DispatchQueue.SchedulerTimeType.Stride { _minimumTolerance } |
| 30 | + |
| 31 | + public var immediateAction: [ImmediateAction] = [] |
| 32 | + public func schedule(options: DispatchQueue.SchedulerOptions?, _ action: @escaping () -> Void) { |
| 33 | + immediateAction.append( |
| 34 | + ImmediateAction(options: options, action: action) |
| 35 | + ) |
| 36 | + |
| 37 | + if enableImmediateExecutionIfPossible { |
| 38 | + action() |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public var delayedActions: [DelayedAction] = [] |
| 43 | + public func schedule( |
| 44 | + after: SchedulerTimeType, |
| 45 | + tolerance: SchedulerTimeType.Stride, |
| 46 | + options: SchedulerOptions?, |
| 47 | + _ action: @escaping () -> Void |
| 48 | + ) { |
| 49 | + delayedActions.append( |
| 50 | + DelayedAction( |
| 51 | + after: after, |
| 52 | + tolerance: tolerance, |
| 53 | + options: options, |
| 54 | + action: action |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + if enableImmediateExecutionIfPossible { |
| 59 | + action() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public var delayedIntervalActions: [DelayedIntervalAction] = [] |
| 64 | + public func schedule( |
| 65 | + after: SchedulerTimeType, |
| 66 | + interval: SchedulerTimeType.Stride, |
| 67 | + tolerance: SchedulerTimeType.Stride, |
| 68 | + options : SchedulerOptions?, |
| 69 | + _ action: @escaping () -> Void |
| 70 | + ) -> Cancellable { |
| 71 | + let delayedIntervalAction = DelayedIntervalAction( |
| 72 | + after: after, |
| 73 | + interval: interval, |
| 74 | + tolerance: tolerance, |
| 75 | + options: options, |
| 76 | + action: action |
| 77 | + ) |
| 78 | + delayedIntervalActions.append(delayedIntervalAction) |
| 79 | + return AnyCancellable { [weak self] in |
| 80 | + self?.delayedIntervalActions.removeAll(where: { $0.uuid == delayedIntervalAction.uuid }) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +public extension TestScheduler { |
| 86 | + struct ImmediateAction { |
| 87 | + fileprivate let uuid = UUID() |
| 88 | + |
| 89 | + public let options: DispatchQueue.SchedulerOptions? |
| 90 | + public let action: () -> Void |
| 91 | + } |
| 92 | + |
| 93 | + struct DelayedAction { |
| 94 | + fileprivate let uuid = UUID() |
| 95 | + |
| 96 | + public let after: SchedulerTimeType |
| 97 | + public let tolerance: SchedulerTimeType.Stride |
| 98 | + public let options: SchedulerOptions? |
| 99 | + public let action: () -> Void |
| 100 | + } |
| 101 | + |
| 102 | + struct DelayedIntervalAction { |
| 103 | + fileprivate let uuid = UUID() |
| 104 | + |
| 105 | + public let after: SchedulerTimeType |
| 106 | + public let interval: SchedulerTimeType.Stride |
| 107 | + public let tolerance: SchedulerTimeType.Stride |
| 108 | + public let options: SchedulerOptions? |
| 109 | + public let action: () -> Void |
| 110 | + } |
| 111 | +} |
0 commit comments