I have two observables. Both of them emits a value, and then completes( they dont complete when the value is emited, but after, so their marble diagram would be sth like ---1---|
). I’d like to create an observable from those two observables. This observable should either completes when
1- one of the two provided observables completes, and the other one did not emit any value
2- the two provided observables have emitted a value, and then completed
How can I achieve this behavior ?
Thanks
This is my starting point
const obsC$ = merge(obsA$, obsB$).pipe( ... )
Its marble diagram would be sth like
----obsAEmission---|(a completes)
----obsBEmission---|(b completes)
----obsA---obsB----|(a and b completes)
3
Answers
One way to approach this that may be a bit heavy handed, is to materialize your source observables and dematerialize your merged observable.
There are probably other solutions, but you can give it a try:
UPDATE: Wait for both source observable to complete if both have emitted a value.
It’s the same idea, I just count the number of values and make sure not to materialize a compete too soon.
I feel like what you need is something like this:
Code on Stackblitz
That said, you haven’t specified if you just need to know when the new observable ends or if you want the values in between merged, etc. What I’ve written above doesn’t return any value, I did assume you only wanted to be aware of the final complete as I had to guess
You could create a function that returns an observable with your custom logic (maybe choose a better name than I did 😀):
The idea here is that you subscribe to each observable and keep track if each of them has emitted. When either of them complete, you evaluate whether or not you want to complete the observable.
Here’s a StackBlitz demonstrating this behavior.