首页 > Web开发 > 详细

[RxJS] Handling Multiple Streams with Merge

时间:2016-03-11 01:11:47      阅读:311      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/Answer1215/p/5264111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!