RxJS-相同,没有订阅/ BehaviorSubject?

时间:2020-05-03 10:43:32

标签: rxjs

我正在寻找一种替代方法:

import { interval, BehaviorSubject } from 'rxjs';

const source$ = interval(1000);
const store$ = new BehaviorSubject<number[]>([]);
source$.subscribe((point) => store$.next([...store$.value].concat(point)));

// result
store$.subscribe(console.log);

我确实根本不想使用订阅,所以我猜没有BehaviorSubject。

感谢任何提示。

1 个答案:

答案 0 :(得分:3)

您可以使用扫描运算符。

const source$ = interval(1000);
const store$ = source$.pipe( // or simply interval(1000).pipe(
    scan((result, value) => [...result, value], []),
);

// result
store$.subscribe(console.log); // [0], [0, 1], ...