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

边栏推荐
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
- Make a skylearn high-dimensional dataset_ Circles and make_ moons
- 手机办理长城证券开户靠谱安全吗?
- Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
- Voiceprint Technology (III): voiceprint recognition technology
- 瑞萨RA系列-开发环境搭建
- The meshgrid() function in numpy
- 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
- SQL高级
- 《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
猜你喜欢

Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
![[matlab] image binarization (imbinarize function)](/img/3e/066f460d9f436bbc43ea35e46093e2.jpg)
[matlab] image binarization (imbinarize function)

Applet cloud development joint table data query and application in cloud function

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

On the underlying index principle of MySQL

Simple waterfall effect

Mapping mode of cache
![[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)](/img/f3/a8414635ad39ad405864f5fcf66d6d.jpg)
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)

The meshgrid() function in numpy
随机推荐
The first techo day Tencent technology open day, 628 waiting for you!
Prediction of pumpkin price based on BP neural network
【mysql学习笔记21】存储引擎
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
What functions should smart agriculture applet system design have
Voiceprint Technology (V): voiceprint segmentation and clustering technology
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
51 SCM time stamp correlation function
Is it safe to open an account online? Who can I ask?
x86的编码格式
处理图片类库
[zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)
Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
如何自制一个安装程序,将程序打包生成安装程序的办法
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
Numpy numpy中的meshgrid()函数
Is it safe to open an account in a mobile phone or a securities company?
Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics
Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)
[buuctf.reverse] 121-125