1
votes

I would like to combine several replayable incomplete observables that hold the last value (BehaviorSubject in this case) in a similar way like zip does, but to make the resulting observable emit a value when any source observables emits.

Here's a fiddle:

const foo$ = new Rx.BehaviorSubject('foo');
const bar$ = new Rx.BehaviorSubject('bar');

setTimeout(() => foo$.next('foo 1'), 1000)
setTimeout(() => bar$.next('bar 1'), 2000)

const foobar$ = Rx.Observable.zip(foo$, bar$);

foobar$.subscribe(([foo, bar]) => console.log({ foo, bar }));

Due to how zip operator works, the code above will output:

{foo: "foo", bar: "bar"}

{foo: "foo 1", bar: "bar 1"}

While I would like it to be

{foo: "foo", bar: "bar"}

{foo: "foo 1", bar: "bar"}

{foo: "foo 1", bar: "bar 1"}

How can this be done?

1

1 Answers

3
votes

Use the combineLatest() operator:

const foobar$ = Rx.Observable.combineLatest(foo$, bar$);

Your updated demo: https://jsfiddle.net/d1wo1usx/