I need builder like `callbackFlow` but I do not n...
# coroutines
k
I need builder like
callbackFlow
but I do not need
awaitClose()
callback. What would be a good candidate for it in flow?
c
why not just leaving the
awaitClose()
block empty?
k
For now I am, I was just wondering if there is any other flow builder type that can emit values from non suspend functions.
c
not sure what you mean but
Copy code
fun foo() = flow {
  emit(true)
}
this is a valid “flow”
so actually if you don’t need the
awaitClose()
that
callbackFlow
provides you can use the basic flow builder.
o
What about 'channelFlow'?
k
I do have to implement async callback function inside of that flow builder. Calling
emit
inside of the lambda gives me compile warning in a sense "emit can only be called within suspend function".
@Oleg Yukhnevich I should look into it. Just tried it and nothing is being emitted from within
channelFlow
body, like no one is consuming it. I'll look into it. For now best option seems to just leave
awaitClose()
empty as @Chrimaeon suggested.
👍 1
n
channelFlow
and
callbackFlow
are the same except
callbackFlow
now throws if you don’t call
awaitClose
(it didn't in the past). The channel is closed when the generator lambda completes. That’s why you need
awaitClose
there even if there's nothing to clean up. Btw, if you can change code such that there is a way to clean up in
awaitClose
, I’d go ahead and do I it. Without that you are likely leaking something beyond your coroutine and that can cause subtle bugs ( like a resource used by a leaked listener after it's been closed).
l
You can also use awaitCancellation() FYI.
m