ReactiveX Retry with Multiple Consumers
<p>Quick question, because I feel like I must be missing something.
I'm using rxjs here because it's what I've got in-front of me, this is a general reactiveX question, I believe.</p>

<p>Let's say I have a set of Observables like so:</p>

<pre><code>network_request = some_thing // An observable that produces the result of a network call
event_stream = network_request.flatMapLatest(function(v) {
    return connectToThing(v) // This is another observable that needs v
}) // This uses the result of the network call to form a long-term event-based connection
</code></pre>

<p>So, this works ok.
Problem, though.
Sometimes the connection thing fails.</p>

<p>So, if I do <code>event_stream.retry()</code> it works great. When it fails, it redoes the network call and gets a new <code>v</code> to use to make a new connection.</p>

<h3>Problem</h3>

<p>What happens if I want two things chained off of my <code>network_request</code>?
Maybe I want the UI to do something every time the network call completes, like show something about <code>v</code> in the UI?</p>

<p>I can do:</p>

<pre><code>shared = network_request.share() // Other implementations call this refCount
event_stream = shared.flatMapLatest(...) // same as above
ui_stream = shared.flatMapLatest(...) // Other transformation on network response
</code></pre>

<p>If I didn't do <code>share</code> then it would have made two requests, which isn't what I want, but with <code>share</code>, when <code>event_stream</code> later has an error, it doesn't retry the network request because the refcount is still at 1 (due to <code>ui_stream</code>), so it immediately returns completed.</p>

<h3>What I want</h3>

<p>This is obviously a small example I've made up to explain my confusion.
What I want is that every time the result of <code>event_stream</code> (that long term connection) has an error all of the following happens:</p>

<ol>
<li>the network request is made again</li>
<li>the new response of that request is used to build a new connection and <code>event_stream</code> goes on with new events like nothing happened</li>
<li>that same response is also emitted in <code>ui_stream</code> to lead to further processing</li>
</ol>

<p>This doesn't feel like a complicated thing, so I must just be misunderstanding something fundamental when it comes to splitting / fanning out RX things.</p>

<h3>Workarounds I think I could do but would like to avoid</h3>

<p>I'm looking to export these observables, so I can't just build them again and then say "Hey, here's the new thing". I want <code>event_stream</code> and all the downstream processing to not know there's been a disconnection.
Same for <code>ui_stream</code>. It just got a new value.</p>

<p>I could probably work something out using a <code>Subject</code> as a generation counter that I ping every time I want everything to restart, and put the <code>network_request</code> into a <code>flatMap</code> based on that, so that I can break the <code>share</code>...
But that feels like a really hacky solution, so I feel there has to be a better way than that.</p>

<p>What have I fundamentally misunderstood?</p>