Dyna Dyna - 12 days ago 6
Javascript Question

RxJS5 operator similiar to .combineLatest but fire whenever a single observable emits

I am looking for a way to combine multiple Observables into a flat tuple of scalar values - similar to

.combineLatest()
- but with the exception that it should emit a new value tuple even when no value has been emitted on one of the source observables - yieldung "undefined" in the tuple for those observables that did not yet emit.

Example:

const s1 = new Subject<string>();
const s2 = new Subject<string>();

Observable.combineWithUndefined(s1, s2).subscribe( ([t1, t2]) => {
console.log(t1.toString() + " " + t2.toString());
});

s1.next("Hello");
s2.next("John");

// expected output:
// Hello undefined
// Hello John

Answer

Make the two subjects startWith the undefined value, so when one of then emits the first value, the combineLatest will emit as well and combine that with the start value of the other subject.

Comments