[RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
by as are Rxjs VALUES Through Implement INNER
2023-09-14 09:00:49 时间
switchMap
is mergeMap
that checks for an "inner" subscription. If the "inner" subscription exists, switchMap
unsubscribes from that "inner" subscription which effectively "cancels" any pending pushes.
import { fromEvent, of, Subscriber } from "rxjs" import { scan, delay, mergeMap, switchMap } from "rxjs/operators" class MySwitchMapSubscriber extends Subscriber { innerSubscription constructor(sub, fn) { super(sub) this.fn = fn } _next(value) { console.log(`outer`, value) const o$ = this.fn(value) if (this.innerSubscription) { this.innerSubscription.unsubscribe() } this.innerSubscription = o$.subscribe({ next: value => { console.log(` inner`, value) this.destination.next(value) } }) } } const mySwitchMap = fn => source => source.lift({ call(sub, source) { source.subscribe( new MySwitchMapSubscriber(sub, fn) ) } }) const observable$ = fromEvent( document, "click" ).pipe( scan(i => i + 1, 0), mySwitchMap(value => of(value).pipe(delay(500))) ) const subscriber = { next: value => { console.log(value) }, complete: () => { console.log("done") }, error: value => { console.log(value) } } observable$.subscribe(subscriber)
相关文章
- pymysql return self._by_id[id] KeyError: 255
- NTFS Disk by Omi NTFS for Mac(NTFS 磁盘管理器)
- ORA-30563: outer join operator (+) not allowed in select-list, CONNECT BY, or START WITH ORACLE 报错 故障修复 远程处理
- ORA-32011: cannot restore SPFILE to location already being used by the instance ORACLE 报错 故障修复 远程处理
- MySQL Error number: 3602; Symbol: ER_FIELD_IN_GROUPING_NOT_GROUP_BY; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-01310: requested return type not supported by the lcr_mine function ORACLE 报错 故障修复 远程处理
- ORA-02264: name already used by an existing constraint ORACLE 报错 故障修复 远程处理
- ORA-02840: Open of log file by client failed ORACLE 报错 故障修复 远程处理
- ORA-09330: Session terminated internally by Oracle or by an Oracle DBA ORACLE 报错 故障修复 远程处理
- ORA-13715: “string” is not recognized by ADDM as a parameter name ORACLE 报错 故障修复 远程处理
- ORA-15005: name “string” is already used by an existing alias ORACLE 报错 故障修复 远程处理
- mysql order by 多个字段排序详解数据库
- Mysql 中使用AS进行别名赋值(mysql中的as)
- 分析Oracle中AS的用法(oracle中as的用法)
- 学习Oracle语法:使用AS(oracle语法as)
- byMySQL之Group by数据分组运算(mysqlgroup)
- Oracle中使用别名AS实现快捷查询(oracle别名as)
- 使用MySQL联表查询AS,提高数据处理效率(mysql联表查询as)
- MySQL中AS关键字的含义及用法(mysql中 as的意思)
- MySQL中AS的用法总结(mysql中as用法总结)
- MySQL中AS在哪些场景下有用(mysql中as用在哪里)
- MySQL中使用AS和子查询(mysql中as与子查询)
- MySQL中AS关键字的含义(as在mysql的意思)
- MySQL中AS关键字的使用(as在mysql中的作用)
- MySQL中的AS用法(as在mysql中)
- Oracle中的AS使用方法(as 在oracle)
- Oracle中使用IS AS的变换方式(oracle中is as)
- 深入理解Oracle中的AS关键字用法(oracle中as用法)
- Oracle AS让企业信息管理更轻松(oracle as 长度)