Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rxjs] Combination Operators #24

Open
varHarrie opened this issue Jul 10, 2018 · 0 comments
Open

[Rxjs] Combination Operators #24

varHarrie opened this issue Jul 10, 2018 · 0 comments

Comments

@varHarrie
Copy link
Owner

varHarrie commented Jul 10, 2018

Combination Operators

目录

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|

参考资料

RxJS Operators 详解
30 天精通 RxJS

@varHarrie varHarrie added this to the Posts milestone Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant