[RxJS] Handling Multiple Streams with Merge
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Handling Multiple Streams with Merge相关的知识,希望对你有一定的参考价值。
You often need to handle multiple user interactions set to different streams. This lesson shows hows Observable.merge
behaves like a "logical OR" to have your stream handle one interaction OR another.
After click the start buttion, we wnat the logic that, click stop, it remember the current state, then if click start, continue with the current state.
If click reset, then it restart from 0;
const Observable = Rx.Observable; const startButton = document.querySelector("#start"); const stopButton = document.querySelector("#stop"); const resetButton = document.querySelector("#reset"); const data = {count: 0}; const inc = (acc) => ({count: acc.count + 1}); const reset = (acc) => data; const start$ = Observable.fromEvent(startButton, ‘click‘); const stop$ = Observable.fromEvent(stopButton, ‘click‘); const reset$ = Observable.fromEvent(resetButton, ‘click‘); const interval$ = Observable.interval(500); const intervalThatStop$ = interval$.takeUntil(stop$);
const incOrReset$ = Observable.merge( intervalThatStop$.mapTo(inc), reset$.mapTo(reset) ); start$ .switchMapTo(incOrReset$) .startWith(data) .scan( (acc, curr) => curr(acc)) .subscribe( (x) => console.log(x))
以上是关于[RxJS] Handling Multiple Streams with Merge的主要内容,如果未能解决你的问题,请参考以下文章
[RxJS 6] The Retry RxJs Error Handling Strategy
[RxJS] Error handling operator: catch