杨锴
2025-03-11 90dc3329d1973fda691e357cf4523d5c7c67fa1d
Pods/RxSwift/RxSwift/Schedulers/VirtualTimeScheduler.swift
@@ -24,6 +24,8 @@
    private var nextId = 0
    private let thread: Thread
    /// - returns: Current time.
    public var now: RxTime {
        self.converter.convertFromVirtualTime(self.clock)
@@ -41,6 +43,7 @@
        self.currentClock = initialClock
        self.running = false
        self.converter = converter
        self.thread = Thread.current
        self.schedulerQueue = PriorityQueue(hasHigherPriority: {
            switch converter.compareVirtualTime($0.time, $1.time) {
            case .lessThan:
@@ -106,8 +109,7 @@
     - returns: The disposable object used to cancel the scheduled action (best effort).
     */
    public func scheduleAbsoluteVirtual<StateType>(_ state: StateType, time: VirtualTime, action: @escaping (StateType) -> Disposable) -> Disposable {
        MainScheduler.ensureExecutingOnScheduler()
        ensusreRunningOnCorrectThread()
        let compositeDisposable = CompositeDisposable()
        let item = VirtualSchedulerItem(action: {
@@ -130,12 +132,11 @@
    /// Starts the virtual time scheduler.
    public func start() {
        MainScheduler.ensureExecutingOnScheduler()
        if self.running {
            return
        }
        ensusreRunningOnCorrectThread()
        self.running = true
        repeat {
            guard let next = self.findNext() else {
@@ -170,12 +171,11 @@
    ///
    /// - parameter virtualTime: Absolute time to advance the scheduler's clock to.
    public func advanceTo(_ virtualTime: VirtualTime) {
        MainScheduler.ensureExecutingOnScheduler()
        if self.running {
            fatalError("Scheduler is already running")
        }
        ensusreRunningOnCorrectThread()
        self.running = true
        repeat {
            guard let next = self.findNext() else {
@@ -199,8 +199,7 @@
    /// Advances the scheduler's clock by the specified relative time.
    public func sleep(_ virtualInterval: VirtualTimeInterval) {
        MainScheduler.ensureExecutingOnScheduler()
        ensusreRunningOnCorrectThread()
        let sleepTo = self.converter.offsetVirtualTime(self.clock, offset: virtualInterval)
        if self.converter.compareVirtualTime(sleepTo, self.clock).lessThen {
            fatalError("Can't sleep to past.")
@@ -211,8 +210,7 @@
    /// Stops the virtual time scheduler.
    public func stop() {
        MainScheduler.ensureExecutingOnScheduler()
        ensusreRunningOnCorrectThread()
        self.running = false
    }
@@ -221,6 +219,12 @@
            _ = Resources.decrementTotal()
        }
    #endif
    private func ensusreRunningOnCorrectThread() {
        guard Thread.current == thread else {
            rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
        }
    }
}
// MARK: description