Skip to content

Commit

Permalink
Add attribution for RetryWithDelay and fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengees committed Apr 18, 2018
1 parent 6afa3da commit 4a71f92
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/kotlin/me/proxer/app/util/RxRetryWithDelay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import io.reactivex.Flowable
import io.reactivex.functions.Function
import java.util.concurrent.TimeUnit

/**
* Stolen and adjusted from here: https://stackoverflow.com/a/25292833/4279995
*/
class RxRetryWithDelay(
private val maxRetries: Int,
private val retryDelayMillis: Long
private val maxRetries: Int,
private val retryDelayMillis: Long
) : Function<Flowable<out Throwable>, Flowable<*>> {

private var retryCount: Int = 0

override fun apply(attempts: Flowable<out Throwable>): Flowable<Any> = attempts
.flatMap { throwable ->
when {
++retryCount < maxRetries -> Flowable.timer(retryDelayMillis, TimeUnit.MILLISECONDS)
else -> Flowable.error(throwable)
}
.flatMap { throwable ->
when {
++retryCount < maxRetries -> Flowable.timer(retryDelayMillis, TimeUnit.MILLISECONDS)
else -> Flowable.error(throwable)
}
}
}

0 comments on commit 4a71f92

Please sign in to comment.