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.
Let's say I have a set of Observables like so:
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
So, this works ok. Problem, though. Sometimes the connection thing fails.
So, if I do event_stream.retry()
it works great. When it fails, it redoes the network call and gets a new v
to use to make a new connection.
Problem
What happens if I want two things chained off of my network_request
?
Maybe I want the UI to do something every time the network call completes, like show something about v
in the UI?
I can do:
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
If I didn't do share
then it would have made two requests, which isn't what I want, but with share
, when event_stream
later has an error, it doesn't retry the network request because the refcount is still at 1 (due to ui_stream
), so it immediately returns completed.
What I want
This is obviously a small example I've made up to explain my confusion.
What I want is that every time the result of event_stream
(that long term connection) has an error all of the following happens:
- the network request is made again
- the new response of that request is used to build a new connection and
event_stream
goes on with new events like nothing happened - that same response is also emitted in
ui_stream
to lead to further processing
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.
Workarounds I think I could do but would like to avoid
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 event_stream
and all the downstream processing to not know there's been a disconnection.
Same for ui_stream
. It just got a new value.
I could probably work something out using a Subject
as a generation counter that I ping every time I want everything to restart, and put the network_request
into a flatMap
based on that, so that I can break the share
...
But that feels like a really hacky solution, so I feel there has to be a better way than that.
What have I fundamentally misunderstood?