[RxJS] Resubscribing to a Stream with Repeat
2024-08-25 16:13:19
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.
最新文章
- css3 FlexBox 弹性布局
- 添加系统右键菜单项 管理员取得所有权(W)(带盾牌)
- 浅谈对ionic项目的理解
- simple_html_dom配合snoopy使用
- 第一课JAVA开发环境配置
- JQuery事件与应用(一)
- django(二)视图和URL配置
- 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
- 如何将 MFC ActiveX 控件标记为安全,脚本和初始化
- CSS常用字体名称
- Ionic生命周期与注意点
- 蚂蚁金服 Service Mesh 渐进式迁移方案|Service Mesh Meetup 实录
- NOIP模拟赛-2018.11.5
- vim_action
- LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别
- P2770 航空路线问题
- Python学习-day14-HTML
- Unity学习之路——C#相关
- 小试JVM工具
- hadoop wordcount程序缺陷