Skip to content

Instantly share code, notes, and snippets.

@isaac-weisberg
Created November 9, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaac-weisberg/8db05888649d71cc8fa1aa4c2eb61be1 to your computer and use it in GitHub Desktop.
Save isaac-weisberg/8db05888649d71cc8fa1aa4c2eb61be1 to your computer and use it in GitHub Desktop.
func a() {
let first = Observables.just(())
let second = Observables.just(())
// will use the super-prime implementation
// because both are sync and all sync
// schedulers and synchronized in relation
// to each other
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func b() {
let first = Observables.just(())
.observeOn(MainScheduler.instance)
let second = Observables.just(())
.observeOn(MainScheduler.instance)
// will use the super-prime implementation
// because two instances of MainScheduler
// and their underlying main queues
// are referrentially equal and thus synchronized
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func c() {
let first = Observables.just(())
.observeOn(SerialDispatchQScheduler())
let second = Observables.just(())
.observeOn(SerialDispatchQScheduler())
// won't use the super-prime implementation
// because although schedulers are
// type equivalent, on runtime their queues
// are will be different and synchronization
// will be required.
// the synchronization will be performed
// on a separate serial dispatch queue
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func d() {
let queue = DispatchQueue(label: "", attributes: .concurrent)
let first = Observables.just(())
.observeOn(DispatchQueueScheduler(queue: queue))
let second = Observables.just(())
.observeOn(DispatchQueueScheduler(queue: queue))
// Won't use the super-prime implementation.
// Concurrent dispatch queues aren't synchronized
// and all emissions will go through a mutex
// Furthermore, the signaure is demoted to
// an unscheduled observable
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func e() {
let scheduler = SerialDispatchQScheduler()
let first = Observables.just(())
.observeOn(scheduler)
let second = Observables.just(())
.observeOn(scheduler)
// will use the super-prime implementation
// because of static and dynamic equality
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func f() {
let first = Observables.create { (handler: Handler<Void>) -> Disposable in
return DisposableVoid()
}
let second = Observables.create { (handler: Handler<Void>) -> DisposableVoid in
return DisposableVoid()
}
// will use the sub-prime implementation
// with a proper mutex
// because that's what we do to regular
// observables
_ = Observables.combineLatest(first, second) { _, _ in () }
}
func g() {
let first = Observables.just(())
.observeOn(MainScheduler.instance)
let second = Observables.just(())
.observeOn(DispatchQueueScheduler(queue: .global()))
// will use the sub-prime implementation
// The type will be demoted to an unscheduled one
// The mutex will be used
_ = Observables.combineLatest(first, second) { _, _ in () }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment