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. Android 属性动画(二)

    当然了属性动画还有一部分的知识点,也能做出很不错的效果,将在本篇博客为您展示~ 1.如何使用xml文件来创建属性动画 大家肯定都清楚,View Animator .Drawable Animator都 ...

  2. (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换

    一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...

  3. Responsive Table 利用@media

    html <table> <thead> <tr> <th>First Name</th> <th>Last Name</ ...

  4. SqlServer 不同服务器之间数据库连接、数据库登录、数据传递

    需求:我是本地数据库想纯SQL访问其它服务器上的数据库,而不使用数据库客户端的连接.这里面就想到了数据库link,通过下面的代码进行创建以后,就可以在本地对链接的服务器数据库进行操作了--添加SQLS ...

  5. java 后台线层也叫守护线层

    import java.util.concurrent.locks.*; class Do9 { public static void main(String[] args) { Ds d=new D ...

  6. C# 前台线程与后台线程区别

    using System; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace Wi ...

  7. Spring MVC 之 Hello World

    1.新建一个动态web项目 2.web.xml编写 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  8. Floyed算法 最短路径

    #include<iostream>#include<cstdio>int v,e,n; //v是顶点数,e是条数int v1[101][101],path[101][101] ...

  9. javascript一些常用函数

    1.indexof 方法可返回某个指定的字符串值在字符串中首次出现的位置. 注释:indexOf() 方法对大小写敏感! 如果要检索的字符串值没有出现,则该方法返回 -1.  例 : 在本例中,我们将 ...

  10. JVM调优总结 + jstat 分析(转)

    [转] JVM调优总结 + jstat 分析 JVM调优总结 + jstat 分析 jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause pid 2000 每格2秒 ...