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

时间: 2024-10-14 22:08:36

[RxJS] Displaying Initial Data with StartWith的相关文章

[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; let ary = Observable.fromArray([1,2,5,4,6]); ary .filter((item) => item % 2===1) .map((item)=> item + "!") .forEach((item) => console.lo

Use Goldengate Initial Data Load

我们在搭建ogg的时候,通常需要先对目标库进行初始化,那么初始化的方式很多.现在介绍ogg自己的初始化方式. 测试环境: Souce DB: OS:redhat 6.3 Name:zbdba1 DB:11.2.0.4 OGG:11.2.1.0.1 Target DB: OS:redhat 6.3 Name:zbdba2 DB:11.2.0.4 OGG:11.2.1.0.1 这里着重介绍 Initial Load,具体安装配置请参考: http://blog.csdn.net/zbdba/arti

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

Note Before proceeding, take a moment to review the Inherit from the Business Class Library Class (EF) lesson. 注意在继续之前,请花点时间复习从商务舱库类 (EF) 课程继承. Open the Updater.cs (Updater.vb) file, located in the MySolution.Module project's Database Update folder.

Indexing Sensor Data

In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set of sensor data that includes sensor data aggregated together from sensors in the sensor network, one or more time stamps for the sensor data, and metad

[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/index.js * Created by wanzhen on 7.6.2016. */ import { v4 } from 'node-uuid'; // This is a fake in-memory implementation of something // that would be

[SAP ABAP开发技术总结]初始值、空、NULL、INITIAL等问题

目录导航 声明:原创作品,转载时请注明文章来自SAP师太博客,并以超链接形式标明文章原始出处,否则将追究法律责任!原文出自: 20.17.       表字段初始值.NULL等问题... 249 20.17.1.            SE11表设置中的Initial Values. 249 20.17.2.            底层数据库表字段默认值... 249 20.17.3.            ABAP初始值.底层数据库表默认值相互转换... 250 20.17.3.1.     

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

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4292833.html TYPES.DATA.TYPE.LIKE. 42 创建数据类型与变量... 44 TYPES 语句... 44 DATA 语句... 46 CONSTANTS 语句... 48 STATICS 语句... 48 TAB

Action、Category、Data、Extras知识详解

开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的文件等等. 诸如此类,都可以通过设置Intent属性来完成. Intent主要有以下四个重要属性,它们分别为: Action:Action属性的值为一个字符串,它代表了系统中已经定义了一系列常用的动作.通过setAction()方法或在清单文件AndroidManifest.xml中设置.默认

Android开发训练之第五章第三节——Transferring Data Without Draining the Battery

Transferring Data Without Draining the Battery GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.0 (API Level 5) or higher YOU SHOULD ALSO READ Optimizing Battery Life In this class you will learn to minimize the battery life impact of downloads a