[RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using share
so that the same stream can be shared across multiple subscriptions.
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)
.repeat()
.subscribe(
(x)=> document.querySelector('#score').innerHTML = `
${x}
`,
err=> console.log(err),
()=> console.log('complete')
);
Current code has a problem every time we type in the input, we need to click delete keyborad to delete the input. This is not conveninet.
So what we want is every time when the code check whether the value match, then we clean the input:
const runningGame$ = 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})
); runningGame$
.subscribe( (x) => input.value = ""); runningGame$
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.repeat()
.subscribe(
(x)=> document.querySelector('#score').innerHTML = `
${x}
`,
err=> console.log(err),
()=> console.log('complete')
);
So we split the code and add another subscribe to clean the input, but now the problem is everytime it console log twice. This is because we have two subscribe on the runningGame$.
TO solve this problem, we need to share() the stream:
const runnintGame$ = 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})
)
.share();
Now it only log out once, but it doesn't clean the input when we click start again. THis is because we only repeat() this part of code:
runningGame$
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.repeat()
What we need to do is also make runningGame$ (clean the input) stream repeat itself:
// To clean the input
runningGame$
.repeat()
.subscribe( (x) => input.value = "");
----------------------------
Code:
const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan((acc, curr)=> curr(acc)) const runningGame$ = 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})
)
.share(); // To clean the input
runningGame$
.repeat()
.subscribe( (x) => input.value = ""); runningGame$
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.repeat()
.subscribe(
(x)=> document.querySelector('#score').innerHTML = `
${x}
`,
err=> console.log(err),
()=> console.log('complete')
);
[RxJS] Sharing Streams with Share的更多相关文章
- [RxJS] Combining streams in RxJS
Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...
- [RxJS] Aggregating Streams With Reduce And Scan using RxJS
What is the RxJS equivalent of Array reduce? What if I want to emit my reduced or aggregated value a ...
- [RxJS] Combining Streams with CombineLatest
Two streams often need to work together to produce the values you’ll need. This lesson shows how to ...
- [RxJS] Hot Observable, by .share()
.share() is an alias for .publish().refCount(). So if the source is not yet completed, no matter how ...
- RH253读书笔记(5)-Lab 5 Network File Sharing Services
Lab 5 Network File Sharing Services Goal: Share file or printer resources with FTP, NFS and Samba Se ...
- Separate code and data contexts: an architectural approach to virtual text sharing
The present invention provides a processor including a core unit for processing requests from at lea ...
- Methods and systems for sharing common job information
Apparatus and methods are provided for utilizing a plurality of processing units. A method comprises ...
- 说说Golang的使用心得
13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...
- theano学习
import numpy import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar( ...
随机推荐
- 借助共享缓存redis实现分布式锁
新开发的系统须要控制每一个时刻回收缓存的GC线程有且仅仅有一个在执行,假设有多个线程同一时候执行,会造成系统崩溃.假设仅仅有一个JVM进程那么非常好办.简单的借助synchronizedkeyword ...
- linux-Python升级安装
Wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz tar zxvf Python-3.5.0.tar.gz && ...
- mysql常用操作 mysql备份与恢复
先登录mysql ==>mysql -uroot -p 查看数据库的版本 select version(); 查看有哪些库 show datases; 查看当前处于哪个库 select da ...
- 动软代码生成V2.74模版简介
最近发现很多人用动软代码生成,确实方便,有些经验记录下,以后查看回顾. ..\Maticsoft\Codematic2\Template\TemplateFile 为模板文件夹,直接在目录下新建文件夹 ...
- 打开新窗口(window.open)
open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL:可选参数,在窗口中要显示网页的网址或路 ...
- (转)asp.net注册实现下一步
在asp.net中有两种容器控件,其中包括panel和placeholder控件. 使用panel控件可以对控件进行分组.一帮助组织web窗体也的内容,将控件组织在面板中,可提供有关在运行时控件应如何 ...
- RAC 常用维护工具和命令(oracle 10g)
Oracle 的管理可以通过OEM或者命令行接口. Oracle Clusterware的命令集可以分为以下4种: 节点层:osnodes 网络层:oifcfg 集群层:crsctl, ocrchec ...
- Swift--基础(一)基本类型 符号 字符串(不熟的地方)
常量 变量 let age = 20 常量不可变 var num = 24 变量可变 let count:Int = 2 定义类型 Double(count) 类型转换 符号 1.?? let de ...
- JAVADOC 常见使用方法 帮助文档
我们知道Java中有三种注释语句: 1.//用于单行注释. 2./*...*/用于多行注释,从/*开始,到*/结束,不能嵌套. 3./**...*/则是为支持jdk工具javadoc.exe而特有的注 ...
- C++ string类及其函数的讲解
文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...