2016-06-14 09:15:31 -04:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import rx.Observable
|
2016-07-08 12:23:03 -04:00
|
|
|
import rx.Scheduler
|
2016-06-14 09:15:31 -04:00
|
|
|
import rx.functions.Func1
|
2016-07-08 12:23:03 -04:00
|
|
|
import rx.schedulers.Schedulers
|
2016-06-14 09:15:31 -04:00
|
|
|
import java.util.concurrent.TimeUnit.MILLISECONDS
|
|
|
|
|
|
|
|
class RetryWithDelay(
|
|
|
|
private val maxRetries: Int = 1,
|
2016-07-08 12:23:03 -04:00
|
|
|
private val retryStrategy: (Int) -> Int = { 1000 },
|
|
|
|
private val scheduler: Scheduler = Schedulers.computation()
|
2016-06-14 09:15:31 -04:00
|
|
|
) : Func1<Observable<out Throwable>, Observable<*>> {
|
|
|
|
|
|
|
|
private var retryCount = 0
|
|
|
|
|
|
|
|
override fun call(attempts: Observable<out Throwable>) = attempts.flatMap { error ->
|
|
|
|
val count = ++retryCount
|
|
|
|
if (count <= maxRetries) {
|
2016-07-08 12:23:03 -04:00
|
|
|
Observable.timer(retryStrategy(count).toLong(), MILLISECONDS, scheduler)
|
2016-06-14 09:15:31 -04:00
|
|
|
} else {
|
|
|
|
Observable.error(error as Throwable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|