rxjs里scan和mergeScan operators的用法

简介: rxjs里scan和mergeScan operators的用法

mergeScan

Applies an accumulator function over the source Observable where the accumulator function itself returns an Observable, then each intermediate Observable returned is merged into the output Observable.


It’s like scan, but the Observables returned by the accumulator are merged into the outer Observable.


看个区别。


先用scan:


const click$ = fromEvent(document, 'click');

   const one$ = click$.pipe(mapTo(1));

   const seed = 0;

   const count$ = one$.pipe(

   scan((acc, one) => (acc + one), seed),

每次点击ui,会显示当前总的点击次数。


用mergeScan的实现:


const click$ = fromEvent(document, 'click');

const one$ = click$.pipe(mapTo(1));

const seed = 0;

const count$ = one$.pipe(

 mergeScan((acc, one) => of(acc + one), seed),

);

count$.subscribe(x => console.log(x));

image.png

目录
相关文章
|
8月前
|
JavaScript
rxjs Observable filter Operator 的实现原理介绍
rxjs Observable filter Operator 的实现原理介绍
43 0
|
8月前
|
索引
RxSwift操作符take、skip、materialize、withLatestFrom、interval等的使用
RxSwift操作符take、skip、materialize、withLatestFrom、interval等的使用
135 1
rxjs里scan operators的用法
rxjs里scan operators的用法
57 0
rxjs里scan operators的用法
rxjs里merge operators的用法
rxjs里merge operators的用法
72 0
rxjs里merge operators的用法
rxjs里concat operators的用法
rxjs里concat operators的用法
118 0
rxjs里concat operators的用法
rxjs里delay operators的用法
rxjs里delay operators的用法
84 0
rxjs里delay operators的用法
rxjs里concatMap operators的用法
rxjs里concatMap operators的用法
96 0
rxjs里concatMap operators的用法
|
JavaScript
rxjs里distinctUntilChanged operators的用法
rxjs里distinctUntilChanged operators的用法
97 0
rxjs里distinctUntilChanged operators的用法
rxjs里takeWhile operators的用法
rxjs里takeWhile operators的用法
93 0
rxjs里takeWhile operators的用法
rxjs里combineLatest operators的用法
rxjs里combineLatest operators的用法
92 0
rxjs里combineLatest operators的用法