fun Something.observer(): Flow<String> = callbackFlow {
val callback = object : Callback() {
override fun onSuccess() {
trySendBlocking("done")
// or
launch { send("done") }
}
}
addCallback(callback)
}
s
Sam
08/16/2023, 7:13 AM
The difference is less than you might think. If you launch a bunch of coroutines to do your sending, you're effectively buffering the values at the dispatcher. I don't think they're guaranteed to send in the order you scheduled them, though. If you want the values buffered, better to add an explicit
buffer()
to the flow.
Sam
08/16/2023, 7:13 AM
In the absence of either type of buffering, the choice of whether your callback is allowed to block its thread is really up to you, and depends on what code/framework will be executing the callback
Sam
08/16/2023, 7:18 AM
Callback flows come with a buffer by default (per the docs), but you might want to change the size of the buffer if you are concerned about blocking
p
Peter
08/16/2023, 7:26 AM
hm, yeah makes sense. I guess it only depends on if you want to block callback executor ๐
Peter
08/16/2023, 7:27 AM
since callback is buffered, I guess
trySend
should be good in most cases
๐ 1
s
Sam
08/16/2023, 7:28 AM
Until the buffer gets full ๐ ๐ญ
โ๏ธ 1
Sam
08/16/2023, 7:28 AM
Thereโs no real good answer unfortunately
Sam
08/16/2023, 7:29 AM
trySendBlocking
will only block if the buffer is full, so I think itโs generally a decent compromise