2016-06-14 15:15:31 +02:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import rx.Observable
|
2016-07-08 18:23:03 +02:00
|
|
|
import rx.Scheduler
|
2016-06-14 15:15:31 +02:00
|
|
|
import rx.functions.Func1
|
2016-07-08 18:23:03 +02:00
|
|
|
import rx.schedulers.Schedulers
|
2016-06-14 15:15:31 +02:00
|
|
|
import java.util.concurrent.TimeUnit.MILLISECONDS
|
|
|
|
|
|
|
|
class RetryWithDelay(
|
|
|
|
private val maxRetries: Int = 1,
|
2016-07-08 18:23:03 +02:00
|
|
|
private val retryStrategy: (Int) -> Int = { 1000 },
|
|
|
|
private val scheduler: Scheduler = Schedulers.computation()
|
2016-06-14 15:15:31 +02: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 18:23:03 +02:00
|
|
|
Observable.timer(retryStrategy(count).toLong(), MILLISECONDS, scheduler)
|
2016-06-14 15:15:31 +02:00
|
|
|
} else {
|
|
|
|
Observable.error(error as Throwable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|