Updates `Scheduler` documentation.
This commit is contained in:
parent
435a84ca25
commit
439ca291bc
|
|
@ -12,7 +12,9 @@ All notable changes to this project will be documented in this file.
|
|||
* use cases like `cell.rx_sentMessage("prepareForReuse")` are now supported.
|
||||
* Linux support (proof of concept, but single threaded mode works)
|
||||
* more info in [Documentation/Linux.md](https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Linux.md)
|
||||
* Initial support for `Swift Package Manager` (works on `Linux`, still can't compile `RxCocoa` on `OSX`)
|
||||
* Initial support for `Swift Package Manager`
|
||||
* works on `Linux` (`RxSwift`, `RxBlocking`, `RxTests`)
|
||||
* doesn't work on OSX because it can't compile `RxCocoa` and `RxTests` (because of inclusion of `XCTest` extensions), but OSX has two other package managers and manual method.
|
||||
* Project content is linked to `Sources` automagically using custom tool
|
||||
* Adds `VirtualTimeScheduler` to `RxSwift`
|
||||
* Adds `HistoricalScheduler` to `RxSwift`
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@
|
|||
location = "group:common.sh">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:package-swift-manager.swift">
|
||||
location = "group:/Users/kzaher/Projects/RxSwift/scripts/package-spm.swift">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:pre-release-tests.sh">
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@
|
|||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
buildConfiguration = "Release"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Release-Tests"
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
|
|
@ -52,7 +52,7 @@
|
|||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
buildConfiguration = "Release"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@
|
|||
import Foundation
|
||||
|
||||
/**
|
||||
Type that represents time interval in the context of this scheduler.
|
||||
Type that represents time interval in the context of RxSwift.
|
||||
*/
|
||||
public typealias RxTimeInterval = NSTimeInterval
|
||||
|
||||
/**
|
||||
Type that represents absolute time in the context of this scheduler.
|
||||
Type that represents absolute time in the context of RxSwift.
|
||||
*/
|
||||
public typealias RxTime = NSDate
|
||||
|
||||
|
|
|
|||
|
|
@ -8,10 +8,17 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
Provides a virtual time scheduler that uses `NSDate` for absolute time and `NSTimeInterval` for relative time.
|
||||
*/
|
||||
public class HistoricalScheduler : VirtualTimeScheduler<HistoricalSchedulerTimeConverter> {
|
||||
|
||||
/**
|
||||
Creates a new historical scheduler with initial clock value.
|
||||
|
||||
- parameter initialClock: Initial value for virtual clock.
|
||||
*/
|
||||
public init(initialClock: RxTime = NSDate(timeIntervalSince1970: 0)) {
|
||||
//print(initialClock)
|
||||
super.init(initialClock: initialClock, converter: HistoricalSchedulerTimeConverter())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,30 +8,68 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
Converts historial virtual time into real time.
|
||||
|
||||
Since historical virtual time is also measured in `NSDate`, this converter is identity function.
|
||||
*/
|
||||
public struct HistoricalSchedulerTimeConverter : VirtualTimeConverterType {
|
||||
/**
|
||||
Virtual time unit used that represents ticks of virtual clock.
|
||||
*/
|
||||
public typealias VirtualTimeUnit = RxTime
|
||||
|
||||
/**
|
||||
Virtual time unit used to represent differences of virtual times.
|
||||
*/
|
||||
public typealias VirtualTimeIntervalUnit = RxTimeInterval
|
||||
|
||||
/**
|
||||
Returns identical value of argument passed because historical virtual time is equal to real time, just
|
||||
decoupled from local machine clock.
|
||||
*/
|
||||
public func convertFromVirtualTime(virtualTime: VirtualTimeUnit) -> RxTime {
|
||||
return virtualTime
|
||||
}
|
||||
|
||||
/**
|
||||
Returns identical value of argument passed because historical virtual time is equal to real time, just
|
||||
decoupled from local machine clock.
|
||||
*/
|
||||
public func convertToVirtualTime(time: RxTime) -> VirtualTimeUnit {
|
||||
return time
|
||||
}
|
||||
|
||||
/**
|
||||
Returns identical value of argument passed because historical virtual time is equal to real time, just
|
||||
decoupled from local machine clock.
|
||||
*/
|
||||
public func convertFromVirtualTimeInterval(virtualTimeInterval: VirtualTimeIntervalUnit) -> RxTimeInterval {
|
||||
return virtualTimeInterval
|
||||
}
|
||||
|
||||
/**
|
||||
Returns identical value of argument passed because historical virtual time is equal to real time, just
|
||||
decoupled from local machine clock.
|
||||
*/
|
||||
public func convertToVirtualTimeInterval(timeInterval: RxTimeInterval) -> VirtualTimeIntervalUnit {
|
||||
return timeInterval
|
||||
}
|
||||
|
||||
public func addVirtualTimeAndTimeInterval(time time: VirtualTimeUnit, timeInterval: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time.dateByAddingTimeInterval(timeInterval)
|
||||
/**
|
||||
Offsets `NSDate` by time interval.
|
||||
|
||||
- parameter time: Time.
|
||||
- parameter timeInterval: Time interval offset.
|
||||
- returns: Time offsetted by time interval.
|
||||
*/
|
||||
public func offsetVirtualTime(time time: VirtualTimeUnit, offset: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time.dateByAddingTimeInterval(offset)
|
||||
}
|
||||
|
||||
/**
|
||||
Compares two `NSDate`s.
|
||||
*/
|
||||
public func compareVirtualTime(lhs: VirtualTimeUnit, _ rhs: VirtualTimeUnit) -> VirtualTimeComparison {
|
||||
switch lhs.compare(rhs) {
|
||||
case .OrderedAscending:
|
||||
|
|
|
|||
|
|
@ -8,17 +8,60 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
Parametrization for virtual time used by `VirtualTimeScheduler`s.
|
||||
*/
|
||||
public protocol VirtualTimeConverterType {
|
||||
/**
|
||||
Virtual time unit used that represents ticks of virtual clock.
|
||||
*/
|
||||
typealias VirtualTimeUnit
|
||||
|
||||
/**
|
||||
Virtual time unit used to represent differences of virtual times.
|
||||
*/
|
||||
typealias VirtualTimeIntervalUnit
|
||||
|
||||
/**
|
||||
Converts virtual time to real time.
|
||||
|
||||
- parameter virtualTime: Virtual time to convert to `NSDate`.
|
||||
- returns: `NSDate` corresponding to virtual time.
|
||||
*/
|
||||
func convertFromVirtualTime(virtualTime: VirtualTimeUnit) -> RxTime
|
||||
|
||||
/**
|
||||
Converts real time to virtual time.
|
||||
|
||||
- parameter time: `NSDate` to convert to virtual time.
|
||||
- returns: Virtual time corresponding to `NSDate`.
|
||||
*/
|
||||
func convertToVirtualTime(time: RxTime) -> VirtualTimeUnit
|
||||
|
||||
/**
|
||||
Converts from virtual time interval to `NSTimeInterval`.
|
||||
|
||||
- parameter virtualTimeInterval: Virtual time interval to convert to `NSTimeInterval`.
|
||||
- returns: `NSTimeInterval` corresponding to virtual time interval.
|
||||
*/
|
||||
func convertFromVirtualTimeInterval(virtualTimeInterval: VirtualTimeIntervalUnit) -> RxTimeInterval
|
||||
|
||||
/**
|
||||
Converts from virtual time interval to `NSTimeInterval`.
|
||||
|
||||
- parameter timeInterval: `NSTimeInterval` to convert to virtual time interval.
|
||||
- returns: Virtual time interval corresponding to time interval.
|
||||
*/
|
||||
func convertToVirtualTimeInterval(timeInterval: RxTimeInterval) -> VirtualTimeIntervalUnit
|
||||
|
||||
func addVirtualTimeAndTimeInterval(time time: VirtualTimeUnit, timeInterval: VirtualTimeIntervalUnit) -> VirtualTimeUnit
|
||||
/**
|
||||
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 time: VirtualTimeUnit, offset: VirtualTimeIntervalUnit) -> VirtualTimeUnit
|
||||
|
||||
/**
|
||||
This is aditional abstraction because `NSDate` is unfortunately not comparable.
|
||||
|
|
@ -46,7 +89,9 @@ public enum VirtualTimeComparison {
|
|||
lhs > rhs.
|
||||
*/
|
||||
case GreaterThan
|
||||
}
|
||||
|
||||
extension VirtualTimeComparison {
|
||||
/**
|
||||
lhs < rhs.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ public class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
|
|||
- returns: The disposable object used to cancel the scheduled action (best effort).
|
||||
*/
|
||||
public func scheduleRelativeVirtual<StateType>(state: StateType, dueTime: VirtualTimeInterval, action: StateType -> Disposable) -> Disposable {
|
||||
let time = _converter.addVirtualTimeAndTimeInterval(time: self.clock, timeInterval: dueTime)
|
||||
let time = _converter.offsetVirtualTime(time: self.clock, offset: dueTime)
|
||||
return scheduleAbsoluteVirtual(state, time: time, action: action)
|
||||
}
|
||||
|
||||
|
|
@ -220,7 +220,7 @@ public class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
|
|||
public func sleep(virtualInterval: VirtualTimeInterval) {
|
||||
MainScheduler.ensureExecutingOnScheduler()
|
||||
|
||||
let sleepTo = _converter.addVirtualTimeAndTimeInterval(time: clock, timeInterval: virtualInterval)
|
||||
let sleepTo = _converter.offsetVirtualTime(time: clock, offset: virtualInterval)
|
||||
if _converter.compareVirtualTime(sleepTo, clock).lessThen {
|
||||
fatalError("Can't sleep to past.")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,18 @@
|
|||
import Foundation
|
||||
import RxSwift
|
||||
|
||||
/**
|
||||
Converter from virtual time and time interval measured in `Int`s to `NSDate` and `NSTimeInterval`.
|
||||
*/
|
||||
public struct TestSchedulerVirtualTimeConverter : VirtualTimeConverterType {
|
||||
/**
|
||||
Virtual time unit used that represents ticks of virtual clock.
|
||||
*/
|
||||
public typealias VirtualTimeUnit = Int
|
||||
|
||||
/**
|
||||
Virtual time unit used to represent differences of virtual times.
|
||||
*/
|
||||
public typealias VirtualTimeIntervalUnit = Int
|
||||
|
||||
private let _resolution: Double
|
||||
|
|
@ -19,26 +29,61 @@ public struct TestSchedulerVirtualTimeConverter : VirtualTimeConverterType {
|
|||
_resolution = resolution
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Converts virtual time to real time.
|
||||
|
||||
- parameter virtualTime: Virtual time to convert to `NSDate`.
|
||||
- returns: `NSDate` corresponding to virtual time.
|
||||
*/
|
||||
public func convertFromVirtualTime(virtualTime: VirtualTimeUnit) -> RxTime {
|
||||
return NSDate(timeIntervalSince1970: RxTimeInterval(virtualTime) * _resolution)
|
||||
}
|
||||
|
||||
/**
|
||||
Converts real time to virtual time.
|
||||
|
||||
- parameter time: `NSDate` to convert to virtual time.
|
||||
- returns: Virtual time corresponding to `NSDate`.
|
||||
*/
|
||||
public func convertToVirtualTime(time: RxTime) -> VirtualTimeUnit {
|
||||
return VirtualTimeIntervalUnit(time.timeIntervalSince1970 / _resolution + 0.5)
|
||||
}
|
||||
|
||||
/**
|
||||
Converts from virtual time interval to `NSTimeInterval`.
|
||||
|
||||
- parameter virtualTimeInterval: Virtual time interval to convert to `NSTimeInterval`.
|
||||
- returns: `NSTimeInterval` corresponding to virtual time interval.
|
||||
*/
|
||||
public func convertFromVirtualTimeInterval(virtualTimeInterval: VirtualTimeIntervalUnit) -> RxTimeInterval {
|
||||
return RxTimeInterval(virtualTimeInterval) * _resolution
|
||||
}
|
||||
|
||||
/**
|
||||
Converts from virtual time interval to `NSTimeInterval`.
|
||||
|
||||
- parameter timeInterval: `NSTimeInterval` to convert to virtual time interval.
|
||||
- returns: Virtual time interval corresponding to time interval.
|
||||
*/
|
||||
public func convertToVirtualTimeInterval(timeInterval: RxTimeInterval) -> VirtualTimeIntervalUnit {
|
||||
return VirtualTimeIntervalUnit(timeInterval / _resolution + 0.5)
|
||||
}
|
||||
|
||||
public func addVirtualTimeAndTimeInterval(time time: VirtualTimeUnit, timeInterval: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time + timeInterval
|
||||
/**
|
||||
Adds virtual time and virtual time interval.
|
||||
|
||||
- parameter time: Virtual time.
|
||||
- parameter offset: Virtual time interval.
|
||||
- returns: Time corresponding to time offsetted by virtual time interval.
|
||||
*/
|
||||
public func offsetVirtualTime(time time: VirtualTimeUnit, offset: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time + offset
|
||||
}
|
||||
|
||||
/**
|
||||
Compares virtual times.
|
||||
*/
|
||||
public func compareVirtualTime(lhs: VirtualTimeUnit, _ rhs: VirtualTimeUnit) -> VirtualTimeComparison {
|
||||
if lhs < rhs {
|
||||
return .LessThan
|
||||
|
|
|
|||
|
|
@ -41,8 +41,8 @@ struct TestVirtualSchedulerVirtualTimeConverter : VirtualTimeConverterType {
|
|||
return Int(timeInterval / 10.0)
|
||||
}
|
||||
|
||||
func addVirtualTimeAndTimeInterval(time time: VirtualTimeUnit, timeInterval: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time + timeInterval
|
||||
func offsetVirtualTime(time time: VirtualTimeUnit, offset: VirtualTimeIntervalUnit) -> VirtualTimeUnit {
|
||||
return time + offset
|
||||
}
|
||||
|
||||
func compareVirtualTime(lhs: VirtualTimeUnit, _ rhs: VirtualTimeUnit) -> VirtualTimeComparison {
|
||||
|
|
|
|||
Loading…
Reference in New Issue