Skip to content

Instantly share code, notes, and snippets.

@tcw165
Created May 10, 2019 16:21
Show Gist options
  • Save tcw165/37e38f2c9be6f67fd2f287995cb86005 to your computer and use it in GitHub Desktop.
Save tcw165/37e38f2c9be6f67fd2f287995cb86005 to your computer and use it in GitHub Desktop.
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