That still doesn't do exactly what a throttle shou...
# coroutines
j
That still doesn't do exactly what a throttle should, but you're reinforcing that the answer is that I need to build it myself.
d
Coroutines are still young, I agree there's still what to build, but in Rx it's much harder to build... out of curiosity, what's missing from throttle?
j
it still delays events needlessly
I don't find it any harder to build in Rx–it's basically the same implementation for both
d
The delay doesn't hold up any threads, so there's no waste
It just suspends
j
the waste is the time that's delayed when it doesn't need to be
d
Did you see the 'non-blocking sleep' section in kotlinx.coroutines KEEP document? If you're not holding any threads, I don't see the waste... I think maybe
delay
is maybe a tricky name....
l
@jw Wouldn't
yield()
do the trick?
j
That would prevent the event source from doing work. I want to prevent the consumer from doing work.
@dave08 time is being wasted in invoking delay when it shouldn't be. your code is more like step consumer not a throttler
d
@jw How does Rx not waste that time, I guess the actual implementation of delay is better, but this is in the KEEP doc:
Copy code
private val executor = Executors.newSingleThreadScheduledExecutor {
Thread(it, "scheduler").apply { isDaemon = true }
}
suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): Unit = suspendCoroutine { cont ->
executor.schedule({ cont.resume(Unit) }, time, unit)
}
You're probably aware of this, I'm just wondering for myself, I view the code as avoiding the step consumer that Rx would do behind the scenes...
j
Rx doesn't have this operator
d
Oh I thought you meant
sample
or similar... if you actually implement something, I'd be very interested in seeing it... thanks @jw!