Dynalon
Dynalon

Reputation: 6814

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

Upvotes: 1

Views: 202

Answers (1)

alebianco
alebianco

Reputation: 2555

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.

Upvotes: 2

Related Questions