Cold and hot streams? What is the difference and w...
# feed
e
Cold and hot streams? What is the difference and why Kotlin coroutines need both: https://medium.com/@elizarov/cold-flows-hot-channels-d74769805f9
K 1
👏 6
K 3
👍 32
⏸️ 11
s
@elizarov Can `Flow`s be shared/published/ref-counted, to have multiple collections receive the values from the same flow-lambda?
e
Not now, but nothing prevents you (or us) from adding the corresponding feature.
You can
publish
a channel which is analogue of "sharing" it.
s
I can't find a function called
publish
... On which class/interface is this defined?
g
As I understand,
broadcastIn
is similar to publish, it converts flow to BroadcastChannel
s
See my question about hot
Flow
https://kotlinlang.slack.com/archives/C1CFAFJSK/p1554801575237600 in #coroutines
s
@gildor broadcastIn will start collecting and return a BroadcastChannel. Is there a way to publish/share/ref-count a Flow without collecting and that returns another Flow?
e
You can do
.asFlow()
on a broadcast channel
s
But a BroadcastChannel is hot.... Is there a way to share a cold Flow and use some sort of subscription/collect counting (much like Rx ref-count) or something like the need to call 'connect', to 'start' the cold Flow?
e
By calling
connect
you’ve turned a cold stream into a hot one.
s
Yes, but I can subscribe multiple times (like 'collect' in Flow) without making it hot, until it's time to finally call connect. Only then it becomes hot.
e
We don’t have that yet. What is the use-case? Can you please explain in an issue at http://github.com/kotlin/kotlinx.coroutines/issues
👍 1
s
@elizarov I wrote a
Flow<T>.publish(): ConnectableFlow<T>
extension-function and its initial implementation seems to work quite well (no thread-safety though). Its use-case is the same as the RxJava’s
Flowable.publish()
method. https://gist.github.com/streetsofboston/39c3f8c882c9891b35e0ebc3cd812381 For thing like this, should we also use http://github.com/kotlin/kotlinx.coroutines/issues?
e
Yes. “They have it in RxJava” is not a use-case, though.
s
True 😀 I was a bit lazy and just assumed the use-cases of why RxJava has 'publish' were self-evident
e
It is not evident that
publish
use-cases need to be solved with
publish
operator and don’t have other solutions that should be considered as well
s
Absolutely. I’ll open a feature request on the github’s issues list.
I opened this issue: https://github.com/Kotlin/kotlinx.coroutines/issues/1086 Please be gentle 🙂