Skip to content

Commit

Permalink
Syntactic sugar for UI scheduler and Test scheduler erasable to UI sc…
Browse files Browse the repository at this point in the history
…heduler (pointfreeco#65)

* Syntactic sugar for UI scheduler and Test scheduler erasible to UI scheduler.

Note: UIScheduler has different specialization than AnySchedulerOf, which means UIScheduler.eraseToAnyScheduler() is not compatible with AnySchedulerOf<DispatchQueue>. Likewise, the existing DispatchQueue.test.eraseToAnyScheduler() is not compatible with TestSchedulerOf<DispatchQueue>.

* Update Sources/CombineSchedulers/AnyScheduler.swift

Co-authored-by: Stephen Celis <[email protected]>

* Update Sources/CombineSchedulers/TestScheduler.swift

Co-authored-by: Stephen Celis <[email protected]>

Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
emixb and stephencelis authored Sep 7, 2022
1 parent 1197396 commit d730182
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/CombineSchedulers/AnyScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,19 @@
RunLoop.main.eraseToAnyScheduler()
}
}

extension AnyScheduler
where
SchedulerTimeType == DispatchQueue.SchedulerTimeType,
SchedulerOptions == Never
{
/// The type-erased UI scheduler shared instance.
///
/// The UI scheduler is a scheduler that executes its work on the main
/// queue as soon as possible (avoiding unnecessary thread hops). See
/// `UIScheduler` for more information.
public static var shared: Self {
UIScheduler.shared.eraseToAnyScheduler()
}
}
#endif
8 changes: 8 additions & 0 deletions Sources/CombineSchedulers/TestScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@
.init(now: .init(.init(uptimeNanoseconds: 1)))
}
}

extension UIScheduler {
/// A test scheduler compatible with type erased UI schedulers.
public static var test: TestSchedulerOf<Self> {
// NB: `DispatchTime(uptimeNanoseconds: 0) == .now())`. Use `1` for consistency.
.init(now: .init(.init(uptimeNanoseconds: 1)))
}
}

extension OperationQueue {
/// A test scheduler of operation queues.
Expand Down

0 comments on commit d730182

Please sign in to comment.