当前位置:网站首页>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 停止值的发送。

边栏推荐
- Voiceprint Technology (II): Fundamentals of audio signal processing
- Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
- 8、智慧交通项目(1)
- [design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)
- With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire
- The problem of wirengpi program running permission
- [IOU] intersection over union
- 8. Intelligent transportation project (1)
- 请问在手机上开户股票,还是去证券公司开户安全?
- Matplotlib simple logistic regression visualization
猜你喜欢

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕

On the underlying index principle of MySQL

Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code

Mapping mode of cache

Compile time annotations for custom annotations (retentionpolicy.class)

Title B of the certification cup of the pistar cluster in the Ibagu catalog

Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts

瑞吉外卖项目(二)

Register the jar package as a service to realize automatic startup after startup

Etcd教程 — 第四章 Etcd集群安全配置
随机推荐
通过客户经理的开户二维码开股票账户安全吗?
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
On the underlying index principle of MySQL
[smart agriculture program] smart agriculture small program project is currently popular.
JMeter interface test, associated interface implementation steps (token)
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
[zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
Is it safe to open an account in a mobile phone or a securities company?
Summarize two methods of configuring pytorch GPU environment
[opencv] - Discrete Fourier transform
华泰证券在上面开户安全吗?靠谱吗?
Flitter gets the height of the top status bar
Socket programming -- epoll model
处理图片类库
2022 meisai question a idea sharing
微服务调用组件Ribbon底层调用流程分析
Is the client that gets the scanning code wechat or Alipay
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Why should the terminal retail industry choose the member management system
x86的编码格式