Created
May 10, 2019 16:21
-
-
Save tcw165/37e38f2c9be6f67fd2f287995cb86005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun setupCoffeeMachine(...) { | |
val debounceClicks: Observable<Unit> = btBrewCoffee.clicks() | |
.debounce(250L, TimeUnit.MILLISECONDS) | |
debounceClicks | |
.switchMapSingle { | |
brewCoffee() // Return Single<Coffee> | |
.timeout(3, TimeUnit.MINUTES, Schedulers.computation()) | |
// Retry at position #1 <-------------------------------------- | |
.retryWhen { errorObservable -> | |
// Will attempt 3 retries, where each is back-off 5^attempts | |
errorObservable | |
.zipWith(Observable.range(1, 3) { (_, i) -> i } | |
.flatMap { i -> Observable.timer(Math.pow(5, i), ...) } | |
} | |
} | |
// Retry at position #2 <---------------------------------------------- | |
.retry() | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({ coffee -> | |
Timber.v("A good $coffee makes me a good day!") | |
}, Timber::e) | |
.disposeOnDestroy() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment