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. UDP C/S编程

    UDP C/S编程的步骤如下图所示与TCP C/S通信的区别在于:服务端没有设置监听和等待连接的过程.客户端没有连接服务端的过程.基于UDP的通信时不可靠地,面向无连接的,发送的数据无法确切知道对方收 ...

  2. Java基础知识强化47:StringBuffer类之StringBuffer的三个面试题

    1. 面试题:String,StringBuffer,StringBuilder的区别 ? 答:String是字符串内容不可变的,而StringBuffer和StringBuilder是字符串内容长度 ...

  3. 毕业设计 ASP.Net+EasyUI开发 X X露天矿调度管理信息系统(一)

    开篇介绍关于EasyUI技术,界面部分的一些使用知识,包括控件的赋值.取值.清空,以及相关的使用. 我们知道,一般Web界面包括的界面控件有:单行文本框.多行文本框.密码文本框.下拉列表Combobo ...

  4. FANTASY:In which way do you think the world will end?

    In which way do you think the world will end? The moment you are reading my essay, you are somehow c ...

  5. ON UPDATE CURRENT_TIMESTAMP

    CREATE TABLE time1 (   id SMALLINT,   time1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TI ...

  6. 智能的PHP缩图类

    *作者:落梦天蝎(beluckly)*完成时间:2006-12-18*类名:CreatMiniature*功能:生成多种类型的缩略图*基本参数:$srcFile,$echoType*方法用到的参数:$ ...

  7. Jasper_crosstab_display a value of field in crosstab total row

    1.create a measure <measure name="myField" class="java.lang.String"> <m ...

  8. NGINX配置小随笔

    达到以下效果: 1,特定目录被指定IP访问 2,不是指定的IP地址不能执行URI中特定字符串 3,特定目录中不能执行PHP文件 set $self_visit ''; if ( $request_ur ...

  9. Altium Designer BGA扇出,原理图中PCB的类和布线规则设置

    本帖隐藏的内容 第一,Altium Designer 认识了这么久,没有用过他的自动扇出功能,今天一试,效果还算不错,不过现在还没有找到不扇出没有网络的引脚的方法,我现在讲我的自动扇出步骤给大家说一下 ...

  10. 如何让 Qt 的程序使用 Sleep(主线程没有Sleep函数,但线程可用自带的保护函数Sleep)

    熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...