Hello, got a question on coroutines Flow. Is there...
# coroutines
d
Hello, got a question on coroutines Flow. Is there a way of replicating RxJava 
timeout
 operator on Kotlin Flow? Namely, for my use case, I want to emit values from a Flow downstream, until a timeout happened (timeout duration should reset after every new emission), then stop emitting (no error) ? The closest impl I see to what I need is the 
debounce
 operator, with the diff that I need to close / complete the flow when timeout happens.
e
something like this? https://pl.kotl.in/n9-b2I_7X
d
thank you, this seems like it does the trick. one question: why did you had scope be as a parameter? (would creating a coroutineScope {} inside this function be more appropriate?)
e
can't call coroutineScope {} inside the function, it's not in the right context
kotlinx.coroutines internally has scopedFlow {} but it's not public
b
I saw a similar question on reddit some months back, and came up with this solution: https://gist.github.com/pablobaxter/50cb9873f38bf6a0e2d72c769ac112f3
This will timeout if the upstream takes too long to produce an item, but doesn't account for the time it takes to emit the same item downstream.
Basically will look like:
Copy code
flow {
    emit("bar")
    delay(100)
    emit("baz")
    delay(100)
    emit("domo")
    delay(1000)
    emit("foo")
}.withTimeout(300).collect {
    delay(500)
    println(it)
}
The output would be:
Copy code
> "bar"
> "baz"
> "domo"
> TimeoutException