I have two sources of streams that I want to listen to them. The requirements are:
- If one emits give me also the last value from the second.
- If two of them emits at the same time, don't call the subscribe two times.
The first case is combineLatest, but the second is zip. I need a way to mix combineLatest and zip into one operator.
const { Observable, BehaviorSubject} = Rx;
const movies = {
ids: [],
entities: {}
}
const actors = {
ids: [],
entities: {}
}
const storeOne = new BehaviorSubject(movies);
const storeTwo = new BehaviorSubject(actors);
const movies$ = storeOne.map(state => state.entities).distinctUntilChanged();
const actors$ = storeTwo.map(state => state.entities).distinctUntilChanged();
const both$ = Observable.zip(
movies$,
actors$,
(movies, actors) => {
return {movies, actors};
}
)
both$.subscribe(console.log);
storeOne.next({
...storeOne.getValue(),
entities: {
1: {id: 1}
},
ids: [1]
});
storeTwo.next({
...storeTwo.getValue(),
entities: {
1: {id: 1}
},
ids: [1]
});
The above code works fine when both emits one after the other, but I need to support also a case where one of them emits. (combineLatest)
How can I do that?
Observable.combineLatest(movies$, actors$, (movies, actors) => ({ movies, actors })).auditTime(0)
– cartant