reline
03/19/2025, 5:16 PMscope.launch { channel.send(value) }
instead of channel.trySendBlocking(value)
to avoid blocking in some callback based API?Joffrey
03/19/2025, 5:19 PMscope.launch
would start an asynchronous operation, while trySendBlocking
would synchronously send the value. If your callback fires many times, trySendBlocking
would apply some backpressure to the callback's caller, while scope.launch
would start many coroutines in an unbounded way.Joffrey
03/19/2025, 5:19 PMscope.launch
in this case. If you don't want backpressure, you can use an unlimited buffer for your channel instead. This is much simpler and doesn't require an extra scope just for this.reline
03/19/2025, 5:22 PMkevin.cianfarini
03/19/2025, 5:23 PMJoffrey
03/19/2025, 5:24 PMI would prefer to suspend rather than block when the callback is invoked.That's not for you to decide, but for the author of the callback-based API. If the callback is not a suspend function, then backpressure works by blocking, and you just have to accept it 😉
reline
03/19/2025, 5:24 PM