[Rxjs] Combination Operators
Opened this issue · 0 comments
varHarrie commented
Combination Operators
目录
- combineLatest
- zip
- withLatestFrom
- 待续...
combineLatest
combineLatest(...obserables: Observable[], project: (...values: any[]) => any)
- 当所有
Observable
都送出第一个值时,开始送出 - 之后每个
Observable
送出时,继续送出 - 将所有
Observable
的最新值
,传入project()
,最终推送值为project()
返回值
const source = Rx.Observalbe.interval(500).take(3)
const newest = Rx.Observable.interval(300).take(6)
const example = source.combineLatest(newest, (x, y) => x + y)
example.subscribe((value) => console.log(value))
source : ----0----1----2|
newest : --0--1--2--3--4--5|
combineLatest(newest, (x, y) => x + y)
example: ----01--23-4--(56)--7|
zip
zip(...obserables: Observable[], project: (...values: any[]) => any)
- 当所有
Observable
都送出第一个值时,开始送出 - 之后当所有
Observable
都送出第i个值时,继续送出 - 将所有
Observable
的第i个值
,传入project()
,最终推送值为project()
返回值 - **注意:**当
Observable
之间的推送时间差距太大,中间会cache
大量的值
const source = Rx.Observalbe.interval(500).take(3)
const newest = Rx.Observable.interval(300).take(6)
const example = source.zip(newest, (x, y) => x + y)
example.subscribe((value) => console.log(value))
source : ----0----1----2|
newest : --0--1--2--3--4--5|
zip(newest, (x, y) => x + y)
example: ----0----2----4|
withLatestFrom
withLatestFrom(...obserables: Observable[], project: (...values: any[]) => any)
- 当所有
Observable
都送出第一个值时,开始送出 - 之后只有
源Obsrvable
送出值时,继续送出 - 将所有
Observable
的最新值
,传入project()
,最终推送值为project()
返回值
const source = Rx.Observalbe.interval(500).take(3)
const newest = Rx.Observable.interval(300).take(6)
const example = source.withLatestFrom(newest, (x, y) => x + y)
example.subscribe((value) => console.log(value))
source : ----0----1----2|
newest : --0--1--2--3--4--5|
withLatestFrom(newest, (x, y) => x + y)
example: ----0----3----6|