当前位置:网站首页>Learning notes of rxjs takeuntil operator
Learning notes of rxjs takeuntil operator
2022-06-25 09:46: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)
);
- Use eventSource and eventNumberCount, adopt
withLatestFromPut two Observable Connect , Thus in map Operator in , You can print the even value and even total amount of the current emission at the same time . adopt takeUntil Pass in one only if the total number of even numbers is greater than 5 When the value is emitted Observable, The total number of even numbers can be greater than 5 after , Give Way interval Stop sending values .

边栏推荐
- 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
- Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)
- How to delete a blank page that cannot be deleted in word
- Prediction of pumpkin price based on BP neural network
- Format analysis and explanation of wav file
- 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 an account on the compass?
- CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
- Rxjs TakeUntil 操作符的学习笔记
- A game WP
猜你喜欢

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

TLAB mechanism of JVM object memory allocation and TLAB process in G1

Prediction of pumpkin price based on BP neural network

22 mathematical modeling contest 22 contest C

Japanese online notes for postgraduate entrance examination (9): composition template

Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics

vscode试图过程写入管道不存在

Format analysis and explanation of wav file

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

Remove the mosaic, there's a way, attached with the running tutorial
随机推荐
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
How to "transform" small and micro businesses (II)?
8. Intelligent transportation project (1)
Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
Ruiji takeout project (II)
vscode试图过程写入管道不存在
C语言刷题随记 —— 猴子吃桃
How to make a self-made installer and package the program to generate an installer
I put a two-dimensional code with rainbow candy
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
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
在指南针上面开户好不好,安不安全?
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
Register the jar package as a service to realize automatic startup after startup
Study on correlation of pumpkin price and design of price prediction model based on BP neural network
【mysql学习笔记22】索引
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
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