Skip to content

Commit

Permalink
Move thread assignment to init method.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielt1263 committed Jul 17, 2024
1 parent 16ca28f commit f63eb13
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions RxSwift/Schedulers/VirtualTimeScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>

private var nextId = 0

private var thread: Thread!
private let thread: Thread

/// - returns: Current time.
public var now: RxTime {
Expand All @@ -43,6 +43,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
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:
Expand Down Expand Up @@ -108,11 +109,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
- 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 {
guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

ensusreRunningOnCorrectThread()
let compositeDisposable = CompositeDisposable()

let item = VirtualSchedulerItem(action: {
Expand All @@ -139,11 +136,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
return
}

guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

ensusreRunningOnCorrectThread()
self.running = true
repeat {
guard let next = self.findNext() else {
Expand Down Expand Up @@ -182,11 +175,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
fatalError("Scheduler is already running")
}

guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

ensusreRunningOnCorrectThread()
self.running = true
repeat {
guard let next = self.findNext() else {
Expand All @@ -210,11 +199,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>

/// Advances the scheduler's clock by the specified relative time.
public func sleep(_ virtualInterval: VirtualTimeInterval) {
guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

ensusreRunningOnCorrectThread()
let sleepTo = self.converter.offsetVirtualTime(self.clock, offset: virtualInterval)
if self.converter.compareVirtualTime(sleepTo, self.clock).lessThen {
fatalError("Can't sleep to past.")
Expand All @@ -225,11 +210,7 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>

/// Stops the virtual time scheduler.
public func stop() {
guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

ensusreRunningOnCorrectThread()
self.running = false
}

Expand All @@ -238,6 +219,12 @@ open class VirtualTimeScheduler<Converter: VirtualTimeConverterType>
_ = 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
Expand Down

0 comments on commit f63eb13

Please sign in to comment.