Andrew Ebling
02/19/2021, 9:52 AMoverride fun onOptionsItemSelected(item: MenuItem): Boolean {
return when(item.itemId) {
R.id.refresh -> {
flow {
callbacks?.refreshAllTheThings()
}.debounce(5000)
return true
}
else -> {
return super.onOptionsItemSelected(item)
}
}
}
...however I get the following compilation error: Not enough information to infer type variable T
I’ve gone and looked at the implementation of debounce()
but the examples in the comments suggest what I have above should be sufficient.
Why does flow need type information if I just want to execute one line of code, at most, every 5 seconds?wasyl
02/19/2021, 9:56 AMflow<Unit> { }
it would be enough to compileval clicksChannel = BroadcastChannel<Unit>(1)
fun onOptionsItemSelected() {
clicksChannel.offer(Unit)
}
onCreate() {
lifecycleScope.launchWhenCreated {
clicksChannel
.asFlow()
.debounce(5000)
.collect { refreshAllThings() }
}
}
Andrew Ebling
02/19/2021, 10:19 AMdebounce
is not the behaviour I’m looking for - more like throttle
but seems coroutines doesn’t use this out of the box?debounce
is waiting until the timeout before sending the first eventwasyl
02/19/2021, 10:26 AMthrottle
exists https://github.com/Kotlin/kotlinx.coroutines/issues/1446Andrew Ebling
02/19/2021, 10:26 AMwasyl
02/19/2021, 10:27 AMsample
would help? Not sureAndrew Ebling
02/19/2021, 10:35 AMwasyl
02/19/2021, 12:25 PM