[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))
时间: 2024-12-20 02:22:17

[RxJS] Handling Multiple Streams with Merge的相关文章

[RxJS] Handling a Complete Stream with Reduce

When a stream has completed, you often need to evaluate everything that has happened while the stream was running. This lesson covers how to use reduce to collect values and total up a “score” of this simple game. const Observable = Rx.Observable; co

[Angular 2] Mapping Streams to Values to Affect State

While you have multiple streams flowing into your scan operator, you'll need to map each stream to the specific values you need to update your state the way that you want. This lesson covers using the map operator to determine what the click and inte

Visualize real-time data streams with Gnuplot

源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) projects - writing rather complex code generators. In the ESA project I am currently working on, I am also the technical lead; and I recently faced t

Part of defining a topology is specifying for each bolt which streams it should receive as input

http://storm.apache.org/ [doing for realtime processing what Hadoop did for batch processing ] Apache Storm is a free and open source distributed realtime computation system. Storm makes it easy to reliably process unbounded streams of data, doing fo

react programming

So you're curious in learning this new thing called Reactive Programming, particularly its variant comprising of Rx, Bacon.js, RAC, and others. 相信你们在学习响应式编程这个新技术的时候都会充满了好奇,特别是它的一些变体,包括Rx系列.Bacon.js.RAC和其他的一些变体. Learning it is hard, even harder by the

How To Use Git Source Control with Xcode in iOS 7

Note from Ray: Tutorial Team member Felipe Laso Marsetti has ported this tutorial to iOS 7 as part of the iOS 7 feast. We hope you enjoy! Whether you're a solo developer or working on a team, if you're not using source control for your projects, you

Nutch相关框架安装使用最佳指南(转帖)

Nutch相关框架安装使用最佳指南 Chinese installing and using instruction  -  The best guidance in installing and using  Nutch in China 国内首套免费的<Nutch相关框架视频教程> 土豆在线观看地址:  http://www.tudou.com/home/item_u106249539s0p1.html超清原版下载地址:  http://pan.baidu.com/share/home?u

Detecting diabetic retinopathy in eye images

Detecting diabetic retinopathy in eye images The past almost four months I have been competing in a Kaggle competition about diabetic retinopathy grading based on high-resolution eye images. In this post I try to reconstruct my progression through th

http2协议翻译(转)

超文本传输协议版本 2 IETF HTTP2草案(draft-ietf-httpbis-http2-13) 摘要 本规范描述了一种优化的超文本传输协议(HTTP).HTTP/2通过引进报头字段压缩以及多路复用来更有效利用网络资源.减少感知延迟.另外还介绍了服务器推送规范. 本文档保持对HTTP/1.1的后向兼容,HTTP的现有的语义保持不变. 1 介绍 The Hypertext Transfer Protocol (HTTP) is a wildly successful protocol.