[RxJS] Resubscribing to a Stream with Repeat
2016-03-13 21:16
274 查看
When you complete a stream, there’s no way to restart it, you must resubscribe. This lesson shows how
Current this block of code:
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:
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.
repeatcomes 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 <= 3) .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 + 1, 0) .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 <= 3) .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 + 1, 0) .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.
相关文章推荐
- JSON 数据的系统解析
- 初学JS的感谢
- JavaScript:NaN和Infinity
- javascript
- js简单交互动画,运动吧
- JavaScript深度克隆
- JavaScript异步加载:不只是Ajax
- JavaScript语言精粹(读书笔记)
- 四种参数传递的形式——URL,超链接,js,form表单
- JavaScript基础篇(一)— — 基础
- JSP与servlet之间的传值方式
- 11. jsp与servlet之间页面跳转及参数传递实例
- jsp页面间的传值方法
- 浏览器中的javaScript
- js实现放大镜效果
- html中js实现左边框控制右边框的显示
- JSP
- 使用 Json.Net 对Json文本进行 增删改查
- 文件操作与JSON
- 2016最新手机号码正则、身份证JS正则表达式