我以为我理解combineLatest,但考虑到我的输出 - 我不理解它。我想用combineLatest,只要任何一个可观察者发射,所有可观察者都会发出它们的最后一个值。
(注意:我刚刚执行了take(5)来限制我的控制台输出)
所以,考虑到这个微不足道的例子 -
const int1$ = Rx.Observable.interval(1000).take(5)
const int2$ = Rx.Observable.interval(500).take(5)
const int3$ = Rx.Observable.interval(3000).take(5)
const all$ = Rx.Observable.combineLatest(
int1$, int2$, int3$
)
all$.subscribe(latestValues => {
const [int1, int2, int3] = latestValues;
console.log(`
interval one @ 1000 ${int1},
interval two @ 500 ${int2},
interval three @ 3000 ${int3}
`)
})
我想看看
"
interval one @ 1000 0,
interval two @ 500 1,
interval three @ 3000 0
"
"
interval one @ 1000 1,
interval two @ 500 2,
interval three @ 3000 0
"
"
interval one @ 1000 1,
interval two @ 500 3,
interval three @ 3000 1
"
"
interval one @ 1000 2,
interval two @ 500 4,
interval three @ 3000 1
但我正在
"
interval one @ 1000 2,
interval two @ 500 4,
interval three @ 3000 0
"
"
interval one @ 1000 3,
interval two @ 500 4,
interval three @ 3000 0
"
"
interval one @ 1000 4,
interval two @ 500 4,
interval three @ 3000 0
"
"
interval one @ 1000 4,
interval two @ 500 4,
interval three @ 3000 1
有点困惑。你对我为什么没有看到我期望的想法会很棒!
答案 0 :(得分:2)
http://reactivex.io/documentation/operators/combinelatest.html 只要任何源Observables发出项目
,CombineLatest就会发出一个项目(只要每个源Observable发出至少一个项目)<< == THIS
int3在3000ms之前没有发出任何项目,所以Rx等待它,然后使用最新项目调用onNext
可能的解决方案:尝试使用定时器(在0秒时发出第一个值,然后每n秒发出一次)
const int1$ = Rx.Observable.timer(0,1000).take(5)
const int2$ = Rx.Observable.timer(0,500).take(5)
const int3$ = Rx.Observable.timer(0,3000).take(5)
const all$ = Rx.Observable.combineLatest(
int1$, int2$, int3$
)
all$.subscribe(latestValues => {
const [int1, int2, int3] = latestValues;
console.log(`
interval one @ 1000 ${int1},
interval two @ 500 ${int2},
interval three @ 3000 ${int3}
`)
})