[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 ...
随机推荐
- Android 推断当前Activity是不是最后一个Activity 以及 应用或Activity是否存在
推断当前Activity是最后一个Activity: 在Activity的方法中, 有一个方法isTaskRoot()方法, 这种方法能够推断当前Activity是否是最后一个Activity, 假 ...
- 将apk文件添加到Android模拟器(AVD)中运行
apk不同exe和jar文件,apk需要在安卓系统中运行,单有一个apk文件还是没用,不能直接拖进AVD中(当然可以直接放到安卓系统的手机中) 由于我们的eclipse大都是已经安装好,解压直接使用的 ...
- Java基础知识强化56:经典排序之快速排序(QuickSort)
1. 快速排序的原理: 快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其 ...
- canvas-画蜗牛
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- hdu 2013
水题 AC代码: #include <iostream> using namespace std; int main() { int i,m,n; while(cin>>n) ...
- ORACLE用户操作的一些常用操作总结【weber出品】
一.创建一个表空间 create tablespace pioneer_data datafile '/u01/datafile/pioneer_datadbf' size 100m autoexte ...
- 服务器端操作Cookie[2]
服务器端操作Cookie,主要注意会使用以下三个类: HttpCookie,HttpResponse,HttpRequest 关于HttpCookie: 属性 描述 例子 Domain 获取或设置与此 ...
- 东软实训2-在jsp中使用javaBean
在JSP中可以像使用普通类一样访问JavaBean,在脚本元素中实例化类的对象,调用对象的方法.JSP提供了3个动作元素,和来访问JavaBean. 1.1 动作用于初始化JavaBean,或者定位一 ...
- dijkstra堆优化模板
#include<iostream> #include<cmath> #include<algorithm> #include<cstring> #in ...
- MongoDB 介绍及Windows下安装
一.MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++ ...