I have an Rxjs observable (stream in the code below)that emits observables (subjOne and subjTwo). Each of the inner observables can emit their own values in any order and at any time. My task is to capture values from subjOne until subjTwo emits its first value.
const subjOne = new Subject();
const subjTwo = new Subject();
const stream = Observable.create(observer => {
observer.next(subjOne);
observer.next(subjTwo);
});
stream
.someOperator(subj => subj)
.subscribe(value => console.log('Value: ', value));
Example 1:
subjOne emits values 1 and 2, then subjTwo emits value 3, then subjOne emits 4.
The output should be: 1, 2, 3.
Example 2:
subjTwo emits 1, then subjOne emits 2.
The output should be 1.
switchMap doesn't fit here because it drops the values from subjOne as soon as subjTwo is emitted from stream. Any ideas about how to achieve that? Thanks.
UPDATE: In my actual case there are not only two inner observables -- subjOne and subjTwo -- but a constant stream of them, so manually hardcoding subjOne.takeUntil(subjTwo) is not a viable option.