You often need to update the data flowing through the stream with custom logic based on what you need to output. This lesson covers how to use scan for collecting and updating the outputs as your stream progresses.

EVerytime we click start button now it will start from 0:

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 intervalThatStop$ = interval$.takeUntil(stop$); start$.switchMapTo(intervalThatStop$)
.subscribe( (x) => {
console.log(x);
})

What we want is it should remeber the current state, so that, next time when we start again, it can use our current state:

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$); start$
.switchMapTo(intervalThatStops$)
.scan( (acc) => {
return Object.assign(acc, {count: acc.count + 1})
}, {
count: 0
})
.subscribe((x)=> console.log(x));

[RxJS] Updating Data with Scan的更多相关文章

  1. [Angular 2] Managing State in RxJS with StartWith and Scan

    The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...

  2. MySQL Crash Course #11# Chapter 20. Updating and Deleting Data

    INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...

  3. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  4. rxjs与vue

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

  5. [Angular] Upgrading to RxJS v6

    This is just a learning blog post, check out the talk. 1. Custom pipeable operators: Custom pipeable ...

  6. Create the Data Access Layer

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspn ...

  7. 响应式js库——rxjs

    原文地址:https://rxjs.dev/guide/overview 简介 RxJS 是组合异步以及基于事件的使用可观察者序列的程序类库.它提供一个核心类型,Observable,附属类型(Obs ...

  8. Data Management and Data Management Tools

    Data Management ObjectivesBy the end o this module, you should understand the fundamentals of data m ...

  9. Building Applications with Force.com and VisualForce(Dev401)(十五):Data Management: Data management Overview

    Dev401-016:Data Management: Data management Overview Course Objectives1.List typical data management ...

随机推荐

  1. linux查看文件大小df-du

    1.  显示目前所有文件系统的可用空间及使用情形,h表示使用 GB.MB 等易读的格式 [root@rusky ldap]# df -hFilesystem Size Used Avail Use% ...

  2. 图形绘制 Canvas Paint Path 详解

    图形绘制简介        Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0.大部分2D使用的api都在android.grap ...

  3. bootstrap table使用小记

    bootstrap table是一个非常不错的,基于bootstrap的插件,它扩展和丰富了bootstrap表格的操作,如格式化表格,表格选择器,表格工具栏,分页等等. 最近基于bootstrap开 ...

  4. XAML 名称范围

    XAML 名称范围存储 XAML 定义的对象名称和它们的对等实例之间的关系.此概念类似于其他编程语言和技术中的术语名称范围的更广泛的含义. 定义 XAML 名称范围的方式 XAML 名称范围中的名称使 ...

  5. 华为 oj 表示数字(代码有参考)理解算法设计

    虽然是初级题目,但是 也不是太容易就做出来的 还是用c++ 好些 因为c++ string 是可以存储到缓冲区的, 字符串长度可以变化 参考了某神的代码 和我的思路一样 ,就拿来主义了,挺经典的一道面 ...

  6. DX笔记之六------游戏画面绘图之透明特效的制作方法

    原文链接:http://blog.csdn.net/zhmxy555/article/details/7338082 透明效果 由于所有的图文件都是以矩形来储存的,我们也许会需要把一张怪兽图片贴到窗口 ...

  7. Zepto源码笔记(二)

    uniq(array) 返回不存在重复值的数组 function classRE(name) 判断classCache中是否已存在name,若存在则取出classCache[name];否则存入该类名 ...

  8. php 之 数据访问 增删改查练习题

    练习题内容: 一.查看新闻页面-----主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. PHP内置Web Server探究(二)自定义PHP控制台输出console函数

    我们在开发APP的服务器端,当和APP进行联调时通常需要实时跟踪URL请求和参数的接收情况. 但PHP并没有像Python或Java专有的控制台输出函数,Python的print()和Java的Sys ...

  10. ./configure 时候报错c++ 编译器不能执行

    ./configure时报错:configure: error: C++ compiler cannot create executables .哎,今天重装测试服务器上的系统,设置好IP可以远程访问 ...