I’m looking for an alternative to flow.emit(). I w...
# coroutines
d
I’m looking for an alternative to flow.emit(). I would need of a data structure (a sized circular buffered list) that would be “coroutine-safe” and the “put” operation is not blocking. The producer should not depend on the consumer and, it should be able to emit data even if there are no consumers listening. I was thinking to Channels, but they offers non-suspending send() only when declared Channel.UNLIMITED. Do you have any suggestion?
o
I'm going to assume by "not blocking" you mean "not suspending" -- how do you expect to accomplish this without a potentially unlimited size buffer?
the only other alternative is to drop items somewhere, otherwise it must suspend to wait for the buffer to have free space
d
yes, sorry by non-blocking I meant not suspending. Buffer should be limited, old values should be replaced by fresh ones (as a circular buffer)
something similar to
BufferUtils.synchronizedBuffer(CircularFifoBuffer())
o
I think you'd probably have to implement this yourself, though you might be able to re-use mechanics from Channel.CONFLATED
d
Thanks Octavia, I thought something was already out there… 😞
o
I'm not familiar with any coroutines libraries myself unfortunately, there might be something that I don't know about
personally I haven't yet had the need to go looking
d
z
I think your use case is covered by the upcoming
MutableSharedFlow
and the
BufferOverflow.KEEP_LATEST
strategy. https://github.com/Kotlin/kotlinx.coroutines/issues/2034
d
Thanks @Zach Klippenstein (he/him) [MOD]! I think it will handle my use case …