Skip to content

Instantly share code, notes, and snippets.

@sidepelican
Last active December 19, 2019 17:08
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 sidepelican/4e2d496e482572603362b67c05b5d1be to your computer and use it in GitHub Desktop.
Save sidepelican/4e2d496e482572603362b67c05b5d1be to your computer and use it in GitHub Desktop.
import RxSwift
import RxCocoa
import UIKit
extension SharedSequence {
func assertStartWithEvent(file: StaticString = #file, line: UInt = #line) -> Self {
let deadline = Observable<Element>.create { observer -> Disposable in
fatalError("Event didn't come immediately after subscribe.", file: file, line: line)
}
.delaySubscription(.nanoseconds(0), scheduler: SharingStrategy.scheduler)
return asObservable()
.amb(deadline)
.asSharedSequence(sharingStrategy: SharingStrategy.self, onErrorRecover: { _ in fatalError("Never come here") })
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let driverWithInitialValue = Driver.just(1)
_ = driverWithInitialValue
.assertStartWithEvent()
.drive()
let relay = PublishRelay<Int>()
let driverWithNoReplay = relay.asDriver(onErrorJustReturn: -1)
_ = driverWithNoReplay
.assertStartWithEvent()
.drive()
relay.accept(1)
DispatchQueue.main.async {
relay.accept(1)
}
print("hi")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment