RxJS combine最新困惑
RxJS combineLatest confusion
我以为我理解 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
有点困惑。你对我为什么没有看到我期望的东西的想法会很棒!
http://reactivex.io/documentation/operators/combinelatest.html
只要任何源 Observables 发出一个项目,CombineLatest 就会发出一个项目
(只要每个源 Observables 都发出了至少一项)<<== THIS
int3 在 3000 毫秒之前没有发出任何项目,所以 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}
`)
})
我以为我理解 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
有点困惑。你对我为什么没有看到我期望的东西的想法会很棒!
http://reactivex.io/documentation/operators/combinelatest.html 只要任何源 Observables 发出一个项目,CombineLatest 就会发出一个项目
(只要每个源 Observables 都发出了至少一项)<<== THIS
int3 在 3000 毫秒之前没有发出任何项目,所以 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}
`)
})