question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flow.share operator

See original GitHub issue

The share() operator operates on Flow<T> and returns Flow<T>. It shall have the following semantics. The resulting flow is cold, but when one collector shart collecting from it, the it starts to collect from the upstream flow, activating the emitter upstream. The trick of the share operator is that when additional collectors appear in the downstream, they all “share” the same upstream emitter.

For example, consider the flow:

val flow = flow { 
    var i = 0
    while(true) { 
        delay(1000) 
        println("Emit $i")
        emit(i++)
    }
}

If you launch two collectors:

launch { flow.collect { println("A: got $it") } }
launch { flow.collect { println("B: got $it") } }

Then you shall see “Emit 0 / A: got 0 / Emit 0 / B: got 0 / Emit 1 / A: got 1 / Emit 1 / B: got 1 / …”.

However, if you change the flow to val flow = flow { /* same */ }.share(), then you shall see “Emit 0 / A: got 0 / B: got 0 / Emit 1 / A: got 1 / B: got 1 / …”, that is one emission gets delivered to both collectors.

Now if you need to artificially “start” the shared flow simply to keep it active, then you can always launch a dummy collector: launch { flow.collect {} } that works as a “reference” which is active until you cancel the resulting job.

TBD: Share operator might need some configuration with the respect to how much “history” to keep in memory for “late collectors”. So far it seems that one non-negative integer is enough (with zero – new collector don’t get any history, with one – only the most recent value, with more – the specified number of recent values). What is unclear is what shall be the default value (if any).

UPDATE: It will have to be, actually, a shareIn(scope) operator. Otherwise, the scope it works in will be bound the the first collectors and when this callector is cancelled it will not be able to maintain emissions to other collectors.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:112
  • Comments:62 (28 by maintainers)

github_iconTop GitHub Comments

14reactions
GianfrancoMScommented, Dec 6, 2019

Migrating from RxJava and I just realize that this operator is not yet implemented in Flow. 😢

12reactions
ursusursuscommented, Dec 3, 2019

Just wanted to say, that share (refCount, replay and friends) is last blocker for me to switch to Flow

Read more comments on GitHub >

github_iconTop Results From Across the Web

Things to know about Flow's shareIn and stateIn operators
The Flow.shareIn and Flow.stateIn operators convert cold flows into hot flows: they can multicast the information that comes from a cold upstream flow...
Read more >
shareIn - Kotlin
The shareIn operator runs the upstream flow in a separate coroutine, and buffers emissions from upstream as explained in the buffer operator's description, ......
Read more >
Things to know about Flow's shareIn and ... - Manuel Vivo .dev
Become familiar with the shareIn and stateIn operators by example. The Flow.shareIn and Flow.stateIn operators convert cold flows into hot ...
Read more >
StateFlow and SharedFlow - Android Developers
Using the callbackFlow created in Kotlin flows as an example, instead of having each collector create a new flow, you can share the...
Read more >
Flow Operators - The Ultimate Guide to Kotlin Flows (Part 2)
In this video you'll get to know the most important flow operators to change and affect the emissions.⭐ Get certificates for your future ......
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found