https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

jw

01/01/2018, 4:11 AM
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

dave08

01/01/2018, 5:56 AM
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

jw

01/01/2018, 6:02 AM
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

dave08

01/01/2018, 6:09 AM
The delay doesn't hold up any threads, so there's no waste
It just suspends
j

jw

01/01/2018, 6:12 AM
the waste is the time that's delayed when it doesn't need to be
d

dave08

01/01/2018, 7:33 AM
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

louiscad

01/01/2018, 10:06 PM
@jw Wouldn't
yield()
do the trick?
j

jw

01/02/2018, 12:02 AM
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

dave08

01/02/2018, 3:54 AM
@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

jw

01/02/2018, 4:10 AM
Rx doesn't have this operator
d

dave08

01/02/2018, 7:21 AM
Oh I thought you meant
sample
or similar... if you actually implement something, I'd be very interested in seeing it... thanks @jw!
8 Views