[RxJS] Displaying Initial Data with StartWith
You often need to render out data before you stream begins from a click or another user interaction. This lessons shows how to use startWith to set the initial output before you trigger your stream.
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const stopButton = document.querySelector('#stop');
const start$ = Observable.fromEvent(startButton, 'click');
const interval$ = Observable.interval(1000);
const stop$ = Observable.fromEvent(stopButton, 'click');
const intervalThatStops$ = interval$
.takeUntil(stop$);
const data = {count: 0};
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
.scan( (acc) => {
return Object.assign(acc, {count: acc.count + 1})
})
.subscribe((x)=> console.log(x));
What startWith will do is, before you click the start button, it will set the initial value for scan(), and logout 0 on the screen.
Then when you click the start button, it will increase from 1 to .....
So it means startWith actually will fire subscrie once.
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const stopButton = document.querySelector('#stop');
const start$ = Observable.fromEvent(startButton, 'click');
const interval$ = Observable.interval(1000);
const stop$ = Observable.fromEvent(stopButton, 'click');
const intervalThatStops$ = interval$
.takeUntil(stop$);
const inc = (acc) => ({count: acc.count + 1}); // one line arrow function only ruturn object need ()
const data = {count: 0};
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
.scan( inc )
.subscribe((x)=> console.log(x));
[RxJS] Displaying Initial Data with StartWith的更多相关文章
- Define the Data Model and Set the Initial Data 定义数据模型并设置初始数据
This topic describes how to define the business model and the business logic for WinForms and ASP.NE ...
- [rxjs] Async, handle data over time
If I have an array, and I want to apply filter, map, forEach to it. let Observable = Rx.Observable; ...
- How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据
After you have introduced a data model, you may need to have the application populate the database w ...
- Supply Initial Data提供初始数据 (EF)
Open the Updater.cs (Updater.vb) file, located in the MySolution.Module project's Database Update fo ...
- 运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
原文 http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data ...
- rxjs与vue
原创文章,转载请注明出处 使用vue-rx插件将vue和rxjs联系起来 在main.js中将vue-rx注入vue中 import Vue from 'vue' import App from '. ...
- TYPES、DATA、TYPE、LIKE、CONSTANTS、STATICS、TABLES
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [Redux] Fetching Data on Route Change
We will learn how to fire up an async request when the route changes. A mock server data: /** /api/i ...
- Action、Category、Data、Extras知识具体解释
开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...
随机推荐
- gcc总结【基本用法】【选项】【动静态库】(转)
1.////////////////////////////////////////////////////////////////////////////////////////////////// ...
- Jquery autocomplete 插件使用
轻松实现类似百度输入框联想功能: autocomplete 是一个很厉害的插件,该插件基于jquery,在jquery官网能下载到最新版本. 首先,jQuery UI 是基于 jQuery 的,所以, ...
- 常用元素的属性/方法 attr / val / html /text
常用元素的属性/方法 得到一个元素的高度, $("#myid").height() 得到一个元素的位置, $("#myid").offset() 返回的是一个o ...
- 3.2:pandas数据的导入与导出【CSV,JSON】
一:CSV数据 一]:导入数据 1)从CSV文件读入数据:pd.read_csv("文件名"),默认以逗号为分隔符 D:\data\ex1.csv文件内容: ...
- defer和async
1.decument.wirte不能使用 2.<script src="text.js" type="text/javascript" defer=&qu ...
- 4月13日 php
用php打印乘法表: <html> <head> <title>我的第一个PHP页面</title> </head> <body> ...
- CSS XHTML规范化命名参考
CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...
- 使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。
分布式微服务现在成为了很多公司架构首先项,据我了解,很多java公司架构都是 Maven+Dubbo+Zookeeper基础上扩展的. Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按 ...
- OCR中的倾斜矫正
电面中被问到了做的LPR,简单的介绍了下后又问到了关于如何矫正倾斜角的问题.答得比较含糊,所以今天来补充一下. 倾斜矫正的方法有很多种,包括基于Hough变换的矫正,基于字符投影的倾角矫正,常规线性角 ...
- PHP安装OPENSSL扩展模块
新项目上线时,PHP开发同事反映邮件功能不能正常使用. 原来是用465的SMTP加密端口,不是25端口.那要为当前的PHP安装OPENSSL扩展啦. 还好,网上有很多,弄一个过来就搞定. http:/ ...