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的更多相关文章

  1. 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 ...

  2. [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; ...

  3. 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 ...

  4. Supply Initial Data提供初始数据 (EF)

    Open the Updater.cs (Updater.vb) file, located in the MySolution.Module project's Database Update fo ...

  5. 运用模型绑定和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 ...

  6. rxjs与vue

    原创文章,转载请注明出处 使用vue-rx插件将vue和rxjs联系起来 在main.js中将vue-rx注入vue中 import Vue from 'vue' import App from '. ...

  7. TYPES、DATA、TYPE、LIKE、CONSTANTS、STATICS、TABLES

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. [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 ...

  9. Action、Category、Data、Extras知识具体解释

    开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...

随机推荐

  1. cocos2d-x v3.2 FlappyBird 各个类对象详细代码分析(7)

    今天我们介绍最后两个类 GameOverLayer类 GameLayer类 GameLayer类是整个游戏中最重要的类,由于是整个游戏的中央系统,控制着各个类(层)之间的交互,这个类中实现了猪脚小鸟和 ...

  2. USB2.0速度识别

    我们知道USB2.0向下兼容USB1.x,即高速2.0的hub能支持所有的速度类型的设备,而USB1.x的hub不能支持高速设备(High Speed Device).因此,如果高速设备挂到USB1. ...

  3. LDAP索引及缓存优化

    一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...

  4. html hack 列表

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![ ...

  5. Objective-C 类型判断

    可以通过 isKindOfClass 判断对象的类型 @interface A : NSObject @end @implementation A @end //// @interface AA : ...

  6. Asp.Net--主题/皮肤文件

    主题 是皮肤的集合.皮肤描述了控件应该如何显示,它可以定义样式的属性,图片,颜色等. 如果拥有多个主题,用户可以根据需要选择主题来显示站点,这只需要通过点击按钮,从一个皮肤切换到另一个皮肤. 皮肤文件 ...

  7. Android小试牛刀之遇到的问题

    1.运行出错 创建项目时没有使用Empty Activity,创建. 2.创建第一个工程 选择Empty Activity才会自动创建Hello Word代码块 3.appcompat_v7的说明 在 ...

  8. title滚动

    代码: <html><head><title>滚动的文字在TITLE栏上不停滚动</title><script language=javascri ...

  9. java学习笔记 (1) —— Strut2.3.24环境搭建

    1.打开MyEclipse,添加WebProject,名称为testStruts2 2.配置Tomcat环境. 1) 在窗口——首选项——MyEclipse——Servers下找到Tomcat6.x ...

  10. zendguard安装破解

    http://www.thefox.cn/zend-guard.shtml 到官网下载6.0版本安装后 URL: http://www.zend.com/en/products/guard/downl ...