博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Displaying Initial Data with StartWith
阅读量:6402 次
发布时间:2019-06-23

本文共 1611 字,大约阅读时间需要 5 分钟。

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));

 

转载地址:http://lcnea.baihongyu.com/

你可能感兴趣的文章
java日志包桥接关系图
查看>>
基于Deep Learning 的视频识别方法概览
查看>>
PostgreSQL 如何实现upsert与新旧数据自动分离
查看>>
MySql Blob图片类型存储Bug解决:索引超出了数组界限错误
查看>>
enum枚举类型
查看>>
Android API 中文 (52) —— ZoomButtonsController.OnZoomListener
查看>>
Go语言的可变(不定长)参数函数
查看>>
!+"\v1" 用来“判断浏览器类型”还是用来“IE判断版本”的问题!
查看>>
Linux IO模型漫谈(2)
查看>>
[网摘学习]在Ubuntu上安装和配置OpenStack Nova之二
查看>>
想挖大数据价值,你得先“挖人”!
查看>>
core dump磁盘报警问题排查过程
查看>>
Nginx报 No input file specified. 的问题解决之路
查看>>
Design Pattern: Not Just Mixin Pattern
查看>>
Ubuntu14.04下安装Hadoop2.5.1 (单机模式)
查看>>
kettle入门与实战(视频教程)
查看>>
简单JNI使用demo
查看>>
框架开发管理流程图
查看>>
Java 容器 & 泛型:四、Colletions.sort 和 Arrays.sort 的算法
查看>>
GDB的两个技巧
查看>>