https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kris Wong

02/11/2020, 4:59 PM
anyone aware of a multiplatform way to execute some work after a delay (in order to coalesce multiple requests)?
i suppose i could just use
CoroutineWorker
with a
delay
a

Arkadii Ivanov

02/11/2020, 5:14 PM
What you mean by "coalesce multiple requests"?
k

Kris Wong

02/11/2020, 5:20 PM
n number of requests becomes 1
a

Arkadii Ivanov

02/11/2020, 5:46 PM
Sorry still not clear to me. Do you mean batching multiple consecutive requests (within a time window) into one? Or to execute a request and any following requests should join the first one?
k

Kris Wong

02/11/2020, 7:30 PM
the former
a

Arkadii Ivanov

02/11/2020, 7:51 PM
Then I believe things are a little bit more complicated than just delay. If I understood you correctly. Need to think about it.
So I would do something like this with Reaktive
Copy code
class BatchedLoader(private val loader: () -> String) {

    private val requests = PublishSubject<Unit>()

    init {
        requests
            .throttle(windowMillis = 1000L)
            .observeOn(ioScheduler)
            .map { loader() }
            .observeOn(mainScheduler)
            .subscribe { result: String ->
                // Do something with the result
            }
    }

    fun load() {
        requests.onNext(Unit)
    }
}
🍻 1
2 Views