当前位置:网站首页>Rxjs TakeUntil 操作符的学习笔记
Rxjs TakeUntil 操作符的学习笔记
2022-06-25 09:36:00 【InfoQ】
import { interval, timer } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
//emit value every 1s
const source = interval(1000);
//after 5 seconds, emit value
const timer$ = timer(5000);
//when timer emits after 5s, complete source
const example = source.pipe(takeUntil(timer$));
//output: 0,1,2,3
const subscribe = example.subscribe(val => console.log(val));

const interval = interval(1000);
const clicks = fromEvent(document, 'click');
const result = interval.pipe(takeUntil(clicks));
result.subscribe(x => console.log(x));
// RxJS v6+
import { interval } from 'rxjs/observable/interval';
import { takeUntil, filter, scan, map, withLatestFrom } from 'rxjs/operators';
//emit value every 1s
const source = interval(1000);
//is number even?
const isEven = val => val % 2 === 0;
//only allow values that are even
const evenSource = source.pipe(filter(isEven));
//keep a running total of the number of even numbers out
const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
//do not emit until 5 even numbers have been emitted
const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));
const example = evenSource.pipe(
//also give me the current even number count for display
withLatestFrom(evenNumberCount),
map(([val, count]) => `Even number (${count}) : ${val}`),
//when five even numbers have been emitted, complete source observable
takeUntil(fiveEvenNumbers)
);
/*
Even number (1) : 0,
Even number (2) : 2
Even number (3) : 4
Even number (4) : 6
Even number (5) : 8
*/
const subscribe = example.subscribe(val => console.log(val));
const evenSource = source.pipe(filter(isEven));
const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));
const example = evenSource.pipe(
//also give me the current even number count for display
withLatestFrom(evenNumberCount),
map(([val, count]) => `Even number (${count}) : ${val}`),
//when five even numbers have been emitted, complete source observable
takeUntil(fiveEvenNumbers)
);
- 使用 eventSource 和 eventNumberCount,通过
withLatestFrom将两个 Observable 进行连接,从而在 map Operator 里,可以同时打印出当前发射的偶数值和偶数总量。通过 takeUntil 传入一个只有在偶数总数个数大于 5 时才发射值的 Observable,可以做到偶数总数大于 5 之后,让 interval 停止值的发送。

边栏推荐
- Title B of the certification cup of the pistar cluster in the Ibagu catalog
- 纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
- Data-driven anomaly detection and early warning of 21 May Day C
- Compile time annotations for custom annotations (retentionpolicy.class)
- x86的编码格式
- How to delete a blank page that cannot be deleted in word
- [shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
- [opencv] - input and output XML and yaml files
- 匯付國際為跨境電商賦能:做合規的跨境支付平臺!
- What functions should smart agriculture applet system design have
猜你喜欢

x86的编码格式

manhattan_slam环境配置

2022 meisai question a idea sharing

Etcd教程 — 第四章 Etcd集群安全配置

Format analysis and explanation of wav file

On the underlying index principle of MySQL

汇付国际为跨境电商赋能:做合规的跨境支付平台!

2021mathorcupc topic optimal design of heat dissipation for submarine data center

Numpy numpy中的meshgrid()函数

首期Techo Day腾讯技术开放日,628等你!
随机推荐
Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
sklearn PolynomialFeatures的具体用法
Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code
Is it safe to open a stock account on the compass?
匯付國際為跨境電商賦能:做合規的跨境支付平臺!
Atguigu---18-component
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
Applet cloud development joint table data query and application in cloud function
Benefits and types of cloud network technology
The problem of wirengpi program running permission
《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
Voiceprint Technology (I): the past and present life of voiceprint Technology
3大问题!Redis缓存异常及处理方案总结
Is it safe to open an account on the compass?
瑞吉外卖项目(二)
Encoding format for x86
sklearn 高维数据集制作make_circles 和 make_moons
Compare and explain common i/o models
处理图片类库
[design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)