当前位置:网站首页>Rxjs TakeUntil 操作符的学习笔记
Rxjs TakeUntil 操作符的学习笔记
2022-06-25 15:39:00 【汪子熙】
TakeUntil 的官方文档对这个操作符的解释是:
Emit values until provided observable emits.
即它可以被赋予另一个起锚定作用的 Observable,当该锚定 Observable emit 值时,原始的 Observable 就停止发射值,进入 complete 操作。
看一个实际的例子:
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));
source Observable 每个1秒的时间间隔,发射一个从 0 开始递增间隔为 1 的整数序列。
我们构造了一个 timer Observable,超时时间间隔为 5 秒,也就是说,在第五秒时,该 Observable 会发送一个值。这个 timer Observable 传入 takeUntil,作为一个 notification Observable,五秒钟之后,source Observable 就会停止发射整数。
最后上述程序执行的输出:4 秒之内打印 0~4,然后结束。

另一个例子:
const interval = interval(1000);
const clicks = fromEvent(document, 'click');
const result = interval.pipe(takeUntil(clicks));
result.subscribe(x => console.log(x));
这个例子里,interval 作为原始的 Observable,clicks 作为 notification Observable,整个程序的表现形式是,每个 1 秒有一个递增1的整数序列打印,直至页面发生点击事件时,原始 interval Observable 终止。
看另一个例子:
// 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));
产生一个每隔1秒发射一个偶数的 Observable.
const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
对产生的偶数的个数进行累加。
const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));
当产生的偶数个数大于 5 时,发射值。这个 Observable 作为 takeUntil 的 notification Observable 使用。
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 停止值的发送。
最后的执行效果:

边栏推荐
- Several relationships of UML
- js 给元素添加自定义属性
- Describe your understanding of the evolution process and internal structure of the method area
- Gold three silver four, an article to solve the resume and interview
- TFIDF and BM25
- Is it safe to open a stock account through the account opening link given by the account manager? I want to open an account
- Mapbox map - inconsistent coordinate system when docking GIS layers?
- How to convert a recorded DOM to a video file
- Don't underestimate the integral mall, its role can be great!
- B站付费视频使up主掉粉过万
猜你喜欢

商城风格也可以很多变,DIY 了解一下!

Read the configuration, explain the principle and read the interview questions. I can only help you here...
How to convert a recorded DOM to a video file
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge

商城风格也可以很多变,DIY了解一下!

Source code analysis of nine routing strategies for distributed task scheduling platform XXL job

基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022

MySQL transaction characteristics and implementation principle
Open the box to experience rust, come on!!!

Sleep formula: how to cure bad sleep?
随机推荐
VectorDraw Developer Framework 10.1001 Crack
教务系统开发(PHP+MySQL)
Finally, we can figure out whether the binding event in the tag is bracketed or not
免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!
f_ Read function [easy to understand]
MySQL修改字段语句
Golang open source streaming media audio and video network transmission service -lal
Educational administration system development (php+mysql)
What is the NFT digital collection?
Do you want to go to an outsourcing company? This article will give you a comprehensive understanding of outsourcing pits!
合宙Air32F103CBT6開發板上手報告
What can NFT metauniverse development do?
加载本地cifar10 数据集
What are the reasons why the game industry needs high defense servers?
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
剑指 Offer 07. 重建二叉树
不要小看了积分商城,它的作用可以很大!
Source code analysis of nine routing strategies for distributed task scheduling platform XXL job
Go development team technical leader Russ Cox sends a document to share go's version control history
Continuously improve the overall performance of adaoracle Oracle Oracle