How do i create a "infinite" flow correctly?
I'm trying to create a flow to "infinitely" consume from a messagem bus. For example:
fun receive(interval: Duration) = flow {
while (true) {
delay(interval.toMillis())
val messages = getMessages(interval)
messages.forEach {
println("Emitting $it")
emit(it)
}
}
}
I can consume the flow "forever" like this:
receive(Duration.ofSeconds(1))
.collect {
println(it)
}
Or limit the number of messages that i consume:
val amount = 10
receive(Duration.ofSeconds(1))
.take(amount)
.collect {
println(it)
}
I tested this and the flow "completes", but i do not understand what happens in the second case, do the
while(true)
loop gets suspended forever? Is this a problem?