我试图合并3个不同流中的最新值,而忽略其中一个(或多个)尚未发出任何值的最新值。
const myStream1$:Observable<>=// steam from somewhere,
myStream2$:Observable<string>=// steam from somewhere,
myStream3$:Observable<string>=// steam from somewhere;
Observable.combineLatest(myStream1$, myStream2$, myStream3$)
.do(([valFromStream1, valFromStream2, valFromStream3])=>
{
// I want to do some side-effect operation even if one(or all) of those 3 streams hasn't emit any value yet
})
.takeUntil(terminator$)
.subscribe();
如何实现?
我可以使用下一个逻辑来实现这一点(但是就像sh * t一样丑陋),我想应该有一个运算符可以自动神奇地做到这一点:
const myStream1$:Observable<string>=// steam from somewhere,
myStream2$:Observable<string>=// steam from somewhere,
myStream3$:Observable<string>=// steam from somewhere;
const subj1 = new BehaviorSubject<string>(undefined),
subj2 = new BehaviorSubject<string>(undefined),
subj3 = new BehaviorSubject<string>(undefined);
myStream1$
.do((val)=>{
subj1.next(val);
}).subscribe();
myStream2$
.do((val)=>{
subj2.next(val);
}).subscribe();
myStream3$
.do((val)=>{
subj3.next(val);
}).subscribe();
Observable.combineLatest(subj1, subj2, subj3)
.do(([valFromStream1, valFromStream2, valFromStream3])=>
{
// I want to do some side-effect operation even if one of those 3 streams hasn't emit any value yet
})
.takeUntil(terminator$)
.subscribe();
答案 0 :(得分:0)
您可以强制CombineLatest通过以空值开始每个流来发出,然后可以基于该值是否为空来有条件地执行逻辑
combineLatest(myStream1$.startWith(null),
myStream2$.startWith(null),
myStream3$.startWith(null))
.do(([v1,v2,v3])=>{
....
}).takeUntil(terminator$)