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. Handler Looper 原理 详解

    演示代码 public class MainActivity extends ListActivity {     private TextView tv_info;     private CalT ...

  2. ComboTree( 树型下拉框) 组件

    本节课重点了解EasyUI中Tree(树)组件的使用方法, 这个组件依赖于Combo(下拉框)和 Tree(树)组件.一. 加载方式//class 加载方式<select id="cc ...

  3. ubantu命令安装banner

    banner命令可以输出图形字符 在线yum安装 $ sudo apt-get update;sudo apt-get install sysvbanner

  4. 2.RxJava详解网址http

    RxJava 到底是什么 RxJava 好在哪 API 介绍和原理简析 1) Scheduler 的 API (二) 2) Scheduler 的原理(二) 3) 延伸:doOnSubscribe() ...

  5. Android -------- 从一个Fragment跳转到另一个Fragment

    一.直接getActivity,使用activity的fragmenttransation的replace方法替换 private void changeToAnotherFragment(){ // ...

  6. lvm拉伸逻辑卷分区小总结

    文件系统                                      容量     已用      可用     已用% 挂载点 /dev/mapper/vg_znl-lv_root   ...

  7. 查看Linux相关信息

    1."uname -a ",可显示电脑以及操作系统的相关信息. 2."cat /proc/version",说明正在运行的内核版本. 3."cat / ...

  8. Swift中对计算属性的理解和对之前的补充

    这个功能的重点作用应该是在计算上. 对于一般的属性,要么直接存一个,要么直接读一个,计算属性则可以根据所设置内容,进行一些修改或计算之类的, 比如: import UIKit class sample ...

  9. 织梦DEDECMS网站首页如何实现分页翻页

    织梦DEDECMS模板网站首页如何实现首页分页和翻页 方法如下:(三种方法,自己选择一种来实现分页吧) 第一种:调用ajax和参数的(不推荐)1.必须在DEDE首页模板中的<head>&l ...

  10. Asp.Net页面生命周期--转发(学海无涯)

    一.什么是Asp.Net页面生命周期 当我们在浏览器地址栏中输入网址,回车查看页面时,这时会向服务器端(IIS)发送一个request请求,服务器就会判断发送过来的请求页面,  完全识别 HTTP 页 ...