anyone aware of a multiplatform way to execute som...
# multiplatform
k
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
What you mean by "coalesce multiple requests"?
k
n number of requests becomes 1
a
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
the former
a
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