switchMap相关文章

  • rxjs里switchMap operators的用法
  • 通过rxjs的一个例子, 来学习SwitchMap的使用方法
  • rxjs switchMap的实现原理
  • rxjs的map和switchMap在SAP Spartacus中的应用 -将高阶Observable进行flatten操作

SwitchMap can cancel in-flight network requests.

先引用rxjs官网对SwitchMap的介绍:

The main difference between switchMap and other flattening operators is the cancelling effect. On each emission the previous inner observable (the result of the function you supplied) is cancelled and the new observable is subscribed. You can remember this by the phrase switch to a new observable.

每次emission,之前的inner Observable会被cancel,新的Observable会被subscribe.

可以使用"switch to a new Observable"的方法来帮助记忆switchMap的这一特性。

Typeheads

This works perfectly for scenarios like typeaheads where you are no longer concerned with the response of the previous request when a new input arrives.

This also is a safe option in situations where a long lived inner observable could cause memory leaks, for instance if you used mergeMap with an interval and forgot to properly dispose of inner subscriptions. -

Remember, switchMap maintains only one inner subscription at a time, this can be seen clearly in the first example.

Be careful though, you probably want to avoid switchMap in scenarios where every request needs to complete, think writes to a database. switchMap could cancel a request if the source emits quickly enough. In these scenarios mergeMap is the correct option.

看个例子:

源代码:

  ngOnInit(): void {fromEvent(document, 'click').pipe(// restart counter on every clickswitchMap(() => interval(1000))).subscribe((oe) => console.log('Jerry: ' + oe));}

输出:


switchMap的实现:

应用程序调用subscribe:

每次调用intervals函数,内部都会新建一个Observable对象:

调用了当前的next方法后,计数器加一,然后继续在preriod毫秒后,调度下一次执行:


返回一个新的Observable对象:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

rxjs switchMap的实现原理相关推荐

  1. RxJS switchMap, mergeMap, concatMap,exhaustMap 的比较

    原文:Comprehensive Guide to Higher-Order RxJs Mapping Operators: switchMap, mergeMap, concatMap (and e ...

  2. RxJs SwitchMap 学习笔记

    网址:https://www.learnrxjs.io/learn-rxjs/operators/transformation/switchmap The main difference betwee ...

  3. RxJs map operator 工作原理分析

    使用一个例子来研究 map 操作符的工作原理. 推荐阅读本文之前,先浏览这篇文章RxJs fromEvent 工作原理分析以了解相关知识. 源代码: import { Component, OnIni ...

  4. rxjs里switchMap operators的用法

    switchMap相关文章 rxjs里switchMap operators的用法 通过rxjs的一个例子, 来学习SwitchMap的使用方法 rxjs switchMap的实现原理 rxjs的ma ...

  5. rxjs的map和switchMap在SAP Spartacus中的应用 -将高阶Observable进行flatten操作

    switchMap相关文章 rxjs里switchMap operators的用法 通过rxjs的一个例子, 来学习SwitchMap的使用方法 rxjs switchMap的实现原理 rxjs的ma ...

  6. 通过rxjs的一个例子, 来学习SwitchMap的使用方法

    switchMap相关文章 rxjs里switchMap operators的用法 通过rxjs的一个例子, 来学习SwitchMap的使用方法 rxjs switchMap的实现原理 rxjs的ma ...

  7. RxJs fromEvent 工作原理分析

    fromEvent(this.test, 'click').subscribe((event) => console.log(event)); this.test 的赋值逻辑: this.tes ...

  8. 深入浅出 RxJS 核心原理(源码实现)

    基础概念 js事件库,通过observable进行异步事件管理 基于观察者模式的响应式编程,生产者主动推送多个数据给订阅的消费者处理 使用纯函数保证应用状态的隔离,保证数据纯净性 通过observab ...

  9. rxjs of操作符里subscribeToArray的实现原理示意图及分析

    写一段简单的代码来测试subscribeToArray: test1(){const subscribeToArray = (array) => (subscriber) => {for ...

最新文章

  1. 【转】matlab函数_连通区域
  2. Springboot使用Log4j2的配置详解
  3. 19 | 案例篇:为什么系统的Swap变高了(上)
  4. linux对目录进行操作,Linux 基础:对文件和目录进行操作的 Linux 和 Unix 命令 笔记...
  5. 求特殊方程的正整数解
  6. 【绘图】matlab绘制图形
  7. 守护进程: supervisor使用
  8. Elasticsearch+Hbase实现海量数据秒回查询
  9. 四小龙变“四小虫”?AI独角兽们的AI+故事好不好讲?
  10. 基于Foolnltk的《三体》文本命名实体分析
  11. 用python实现弹跳球游戏_使用Pygame模块制作一个跳跃的小球游戏
  12. 使用Golang计算网络IP地址的CIDR
  13. Queen Collisions(分行列模拟)
  14. 理解MVC、MVP、MVVM在干什么,进化的原因。
  15. 浑身尖刺的服务可用性守护者——hystrix熔断器实践记录
  16. win7防火墙开启ping
  17. 25岁同济硕士生斩获中国首个CVPR最佳学生论文奖,他还是个「赛车发烧友」
  18. 一篇文章让你彻底明白区块链有哪些应用场景
  19. 小米(红米)AC2100 硬路由配置的坑
  20. pboc规范详细解释

热门文章

  1. Java语法糖1:可变长度参数以及foreach循环原理
  2. static_cast
  3. 几种流行Webservice框架性能对照
  4. silverlight 学习笔记 (五): MVVM Light Toolkits 之 RealCommand
  5. 从零写一个编译器(一):输入系统和词法分析
  6. Jython 安装使用
  7. Json-server 创建模拟API服务器
  8. notepad++每行首尾添加内容
  9. 设置UITextField占位符的颜色和字体
  10. 阿里巴巴和印度最大移动支付和商务平台Paytm