杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
//
//  VirtualTimeConverterType.swift
//  RxSwift
//
//  Created by Krunoslav Zaher on 12/23/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
 
import Foundation
 
/// Parametrization for virtual time used by `VirtualTimeScheduler`s.
public protocol VirtualTimeConverterType {
    /// Virtual time unit used that represents ticks of virtual clock.
    associatedtype VirtualTimeUnit
 
    /// Virtual time unit used to represent differences of virtual times.
    associatedtype VirtualTimeIntervalUnit
 
    /**
     Converts virtual time to real time.
     
     - parameter virtualTime: Virtual time to convert to `Date`.
     - returns: `Date` corresponding to virtual time.
    */
    func convertFromVirtualTime(_ virtualTime: VirtualTimeUnit) -> RxTime
 
    /**
     Converts real time to virtual time.
     
     - parameter time: `Date` to convert to virtual time.
     - returns: Virtual time corresponding to `Date`.
    */
    func convertToVirtualTime(_ time: RxTime) -> VirtualTimeUnit
 
    /**
     Converts from virtual time interval to `TimeInterval`.
     
     - parameter virtualTimeInterval: Virtual time interval to convert to `TimeInterval`.
     - returns: `TimeInterval` corresponding to virtual time interval.
    */
    func convertFromVirtualTimeInterval(_ virtualTimeInterval: VirtualTimeIntervalUnit) -> TimeInterval
 
    /**
     Converts from `TimeInterval` to virtual time interval.
     
     - parameter timeInterval: `TimeInterval` to convert to virtual time interval.
     - returns: Virtual time interval corresponding to time interval.
    */
    func convertToVirtualTimeInterval(_ timeInterval: TimeInterval) -> VirtualTimeIntervalUnit
 
    /**
     Offsets virtual time by virtual time interval.
     
     - parameter time: Virtual time.
     - parameter offset: Virtual time interval.
     - returns: Time corresponding to time offsetted by virtual time interval.
    */
    func offsetVirtualTime(_ time: VirtualTimeUnit, offset: VirtualTimeIntervalUnit) -> VirtualTimeUnit
 
    /**
     This is additional abstraction because `Date` is unfortunately not comparable.
     Extending `Date` with `Comparable` would be too risky because of possible collisions with other libraries.
    */
    func compareVirtualTime(_ lhs: VirtualTimeUnit, _ rhs: VirtualTimeUnit) -> VirtualTimeComparison
}
 
/**
 Virtual time comparison result.
 
 This is additional abstraction because `Date` is unfortunately not comparable.
 Extending `Date` with `Comparable` would be too risky because of possible collisions with other libraries.
*/
public enum VirtualTimeComparison {
    /// lhs < rhs.
    case lessThan
    /// lhs == rhs.
    case equal
    /// lhs > rhs.
    case greaterThan
}
 
extension VirtualTimeComparison {
    /// lhs < rhs.
    var lessThen: Bool {
        self == .lessThan
    }
 
    /// lhs > rhs
    var greaterThan: Bool {
        self == .greaterThan
    }
 
    /// lhs == rhs
    var equal: Bool {
        self == .equal
    }
}