当前位置:网站首页>RxJs SwitchMapTo 操作符之移花接木
RxJs SwitchMapTo 操作符之移花接木
2022-08-01 21:54:00 【华为云】
将每个源值投影到同一个 Observable,该 Observable 在输出 Observable 中使用 switchMap 多次展平。
输入一个 Observable,输出一个 function Operator. 实际是一个函数,每次在源 Observable 上发出值时,该函数都会返回一个 新的 Observable.
该函数从给定的 innerObservable 发出项目,并且仅从最近投影的内部 Observable 中获取值。
看个例子:
import { EMPTY, range } from 'rxjs';import { first, take, tap } from 'rxjs/operators';import { fromEvent, interval } from 'rxjs';import { switchMapTo } from 'rxjs/operators';const clicks = fromEvent(document, 'click');const test = event => console.log('Jerry: ', event);const result = clicks.pipe( tap(test), switchMapTo(interval(1000)));result.subscribe(x => console.log(x));输出:
每次点击之后,click$ 抛出的 PointerEvent,被 switchMapTo 返回的 Function Operator 丢弃了。最后用户订阅 result 函数里,打印的值,是 switchMapTo 输入的 interval(1000) Observable 发射的值,而不再是 clicks$ 抛出的 PointerEvent.

再看另一个在网页显示倒计时数字的例子。
import './style.css';import { interval, fromEvent } from 'rxjs';import { switchMapTo, scan, startWith, takeWhile, finalize} from 'rxjs/operators';const COUNTDOWN_TIME = 10;// referenceconst countdownElem = document.getElementById('countdown');// streamsconst click$ = fromEvent(document, 'click');const countdown$ = interval(2000).pipe( scan((acc, _) => --acc, COUNTDOWN_TIME), startWith(COUNTDOWN_TIME));click$ .pipe( switchMapTo(countdown$), takeWhile(val => val >= -10), finalize(() => (countdownElem.innerHTML = "We're done here!")) ) .subscribe((val: any) => (countdownElem.innerHTML = val));初始整数是10,每隔2秒钟减一,减到 -10 时停止。

思路:触发计数器开始递减的操作是点击屏幕,因此需要使用 fromEvent 来构造 Observable :click$
每隔两秒钟执行某项操作,因此需要用 interval 构造第二个 Observable.
一旦计数器启动之后,每隔两秒钟需要执行递减操作,因此需要用 switchMapTo,将 click$ 映射成 interval Observable.
之后的值传递,就和 click$ 再无任何关联了。
因为是递减操作,暗示这是一个 stateful 场景,故选用 scan 操作符维护内部状态。
边栏推荐
- 9. SAP ABAP OData 服务如何支持删除(Delete)操作
- 威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
- 统计单词数
- AQS
- Centos7--MySQL的安装
- Pagoda application experience
- Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
- 罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
- MySQL related knowledge
猜你喜欢

19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform

10 Practical Uses of NFTs (NFT System Development)

HCIP---Architecture of Enterprise Network

Mini Program--Independent Subcontracting & Subcontracting Pre-download

论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》

LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)

365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses

小程序容器+自定义插件,可实现混合App快速开发

Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen

基于php酒店在线预定管理系统获取(php毕业设计)
随机推荐
【ASM】字节码操作 MethodWriter
365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
图像融合GANMcC学习笔记
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
[@synthesize in Objective-C]
AI应用第一课:支付宝刷脸登录
Small program -- subcontracting
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
统计单词数
Spark cluster construction
SQL injection of WEB penetration
ModuleNotFoundError: No module named 'yaml'
Spark集群搭建
Anacoda的用途
AQS
long investment career
第一讲 测试知多少
你居然不懂Bitmap和Drawable? 相关知识大扫盲
dvwa 通关记录1 - 暴力破解 Brute Force