[RxJS] Resubscribing to a Stream with Repeat
When you complete a stream, there’s no way to restart it, you must resubscribe. This lesson shows how repeat
comes in handy to resubscribe after a stream has completed.
Current this block of code:
const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan((acc, curr)=> curr(acc)) timer$
.do((x)=> console.log(x))
.takeWhile((data)=> data.count <= )
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log('Score', x),
err=> console.log(err),
()=> console.log('complete')
); /**
"Score"
0
"complete"
**/
Once it hit complete block, you can never start the timer again. THis is because the stream is completed, so if we want able to re-subscribe many times, we can use repeact() method:
timer$
.do((x)=> console.log(x))
.takeWhile((data)=> data.count <= )
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.repeat() // repact the block of code above
.subscribe(
(x)=> console.log('Score', x),
err=> console.log(err),
()=> console.log('complete')
);
Now we are able to repact the stream, but it will never hit the complete block, but it is ok.
And also should be careful when use repeact(); you cannot put it anywhere you want, if you put it before fiilter(), then it willl never hit the filter block, so usually should put it right before the subscribe() method.
[RxJS] Resubscribing to a Stream with Repeat的更多相关文章
- [RxJS] Handling a Complete Stream with Reduce
When a stream has completed, you often need to evaluate everything that has happened while the strea ...
- [RxJS] Toggle A Stream On And Off With RxJS
This lesson covers how to toggle an observable on and off from another observable by showing how to ...
- [Recompose] Stream Props to React Children with RxJS
You can decouple the parent stream Component from the mapped React Component by using props.children ...
- [Recompose] Stream a React Component from an Ajax Request with RxJS
Loading data using RxJS is simple using Observable.ajax. This lesson shows you how to take the ajax ...
- [Recompose] Pass a React Prop to a Stream in RxJS
When you declare your Component and Props in JSX, you can pass those props along to your RxJS stream ...
- [RxJS] Transformation operator: repeat
Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...
- [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
Higher order Array functions such as filter, map and reduce are great for functional programming, bu ...
- [RxJS] Logging a Stream with do()
To help understand your stream, you’ll almost always want to log out some the intermediate values to ...
- [RxJS] Stopping a Stream with TakeUntil
Observables often need to be stopped before they are completed. This lesson shows how to use takeUnt ...
随机推荐
- 关于js中 document.body.scrollTop 不能返回正确值的原因
本来是为了通过document.body.scrollTop来获取浏览器垂直滚动条向下滚动的像素,但是不管滚动条在什么位置总是返回是0,造成这样的原因和html的头部声明有关,如果头部声明 为:< ...
- SUN-LDAP6.3_RHEL 5.0-卸载LDAP
卸载LDAP 1.注销服务器 到目录/ldap/ldapinstance/dscc6/bin下 # ./dsccreg remove-server -h 主机名 /ldap/ldapinstance/ ...
- HTM5新增结构化元素&非结构化元素&新增属性详解
(1)HTML5 新增的主体结构元素 (2)HTML5 新增的的非主体结构元素 (3)HTML5 表单新增元素与属性 (4)HTML5 改良的 input 元素的种类
- SQL Server 如何创建定时作业
在做SQL server 管理时,往往需要每日执行定时任务,但是如果每天都去人工执行,非常不方便,而且一般定时操作,都应该是在数据库压力不大时,一般是在夜间.所以我们需要创建定时作业来代替人工的执行定 ...
- 关于sqfa
有的时候定义状态,下次来到这个状态的时候,不应该就否决掉下次,因为下次的权值可能比这次更优..
- 如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库
关键的代码和正则表达式在这里 while((line=br.readLine())!=null) { String[] data=new String[4]; data=line.split(&quo ...
- Linux下MySQL的彻底卸载和安装配置字符集
前言: Linux环境下MySQL的安装和配置在网上已经有很多教程了.之所以写这篇文章是因为在配置字符集的时候找了网上的一些教程发现并不能用导致折腾了一阵子.下面的教程均是亲自实践. MySQL的彻底 ...
- CSS阻止文本选择
在日常运用中,经常遇到点击按钮/菜单的时候,选中了文本,为了避免这种情况,可以使用纯css来解决这个问题(IE10+),对于旧版本的就只能用js:onselectstart = 'return fal ...
- javascript之事件绑定
曾经写过一篇随笔,attachEvent和addEventListener,跟本文内容有很多相似之处 本文链接:javascript之事件绑定 1.原始写法 <div onclick=" ...
- .NET winform 在listview中添加progressbar
找了好长时间没找到,后来索性自己写了一个: 首先,在往listview加载数据的事件里添加progressbar: foreach (string d in arr) { ; item = new L ...