[Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
Splitting a stream into multiple streams causes new subscriptions. You can think of new subscriptions as "invoking a function". So if your original stream makes a request for data, splitting the original stream will make 2 requests for data. The way to get around this is by using share so that each time the stream is split, all the split stream stay synced together.
For the RxJS Code below, it issues network reuqest twice:
const createLoader = url => from(this.$http.get(url)).pipe(pluck('data'));
const luke$ = this.click$.pipe(
mapTo('https://starwars.egghead.training/people/1'),
switchMap(createLoader),
catchError(() => createLoader('https://starwars.egghead.training/people/2'))
);
const name$ = luke$.pipe(pluck('name'));
const image$ = luke$.pipe(
pluck('image'),
map(src => `https://starwars.egghead.training/${src}`)
);
Because both 'name$' and 'image$' will trigger 'luke$', then 'createLoader'.
In order to solve the problem we can use 'share()' or 'shareReplay(1)':
const luke$ = this.click$.pipe(
mapTo('https://starwars.egghead.trainin/people/1'),
switchMap(createLoader),
catchError(() => createLoader('https://starwars.egghead.training/people/2')),
share()
);
[Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js的更多相关文章
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...
- vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)
vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...
- vue新手入门指导,一篇让你学会vue技术栈,本人初学时候的文档
今天整理文档突然发现了一份md文档,打开一看 瞬间想起当年学习vue的艰难路,没人指导全靠自己蒙,下面就是md文档内容,需要的小伙伴可以打开个在线的md编译器看一看,我相信不管是新人还是老人 入门总 ...
- Please do not register multiple Pages in undefined.js 小程序报错的几种解决方案
Wed Jun 27 2018 09:25:43 GMT+0800 (中国标准时间) Page 注册错误,Please do not register multiple Pages in undefi ...
- Vue引入第三方JavaScript库和如何创建自己的Vue插件
一 第三方JavaScript库 前言 .vue文件 中不解析 script标签引入js文件,只能用 import 引入 有两种用法: 1.import a from '../a' 2.import ...
- 【js】vue 2.5.1 源码学习 (三) Vue.extend 和 data的合并策略
大体思路 (三) 1. 子类父类 2.Vue.extend() //创建vue的子类 组件的语法器 Vue.extend(options) Profile().$mount('#app' ...
- 循序渐进VUE+Element 前端应用开发(7)--- 介绍一些常规的JS处理函数
在我们使用VUE+Element 处理界面的时候,往往碰到需要利用JS集合处理的各种方法,如Filter.Map.reduce等方法,也可以设计到一些对象属性赋值等常规的处理或者递归的处理方法,以前对 ...
- Vue源码分析之实现一个简易版的Vue
目标 参考 https://cn.vuejs.org/v2/guide/reactivity.html 使用 Typescript 编写简易版的 vue 实现数据的响应式和基本的视图渲染,以及双向绑定 ...
- Vue路由器的hash和history两种工作模式 && Vue项目编译部署
1 # 一.Vue路由器的两种工作模式 2 # 1.对于一个uri来说,什么是hash值? 井号及其后面的内容就是hash值. 3 # 2.hash值不会包括含在HTTP请求中,即:hash值不会带给 ...
随机推荐
- Java 8 (8) 默认方法
传统上,Java程序的接口是将相关方法按照预定组合到一起的方式.实现接口的类必须为接口中定义的方法提供一个实现,或者从父类中集成它的实现.但是,一旦类库的设计者需要更新接口,向接口中加入新的方法时候, ...
- c3p0参数详解
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> <property name="acquireIncrement"& ...
- ERwin逻辑模型
1.自动排序 Format>>Preferences>>Layout Entire Diagram CA ERwin
- UIPageViewController 翻页、新手引导--UIScrollView:pagingEnabled
UIPageViewController 翻页.新手引导--UIScrollView:pagingEnabled
- 梦想CAD控件系统变量说明
这里介绍一些常用系统变量有String.double.long.McGePoint3d等类型,其中有部分系统变量是随图纸保存,再次打开时就会读取图纸中的系统变量,有些系统变量不随图纸保存,其作用来控制 ...
- Java线程处理
Java线程处理 创建线程 继承Thread类 public class TestThread extends Thread{ public void run(){ System.out.printl ...
- BLOCK层基本概念:bio,request,request_queue
Summary bio 代表一个IO 请求 request 是bio 提交给IO调度器产生的数据,一个request 中放着顺序排列的bio 当设备提交bio 给IO调度器时,IO调度器可能会插入bi ...
- 四角递推(CF Working out,动态规划递推)
题目:假如有A,B两个人,在一个m*n的矩阵,然后A在(1,1),B在(m,1),A要走到(m,n),B要走到(1,n),两人走的过程中可以捡起格子上的数字,而且两人速度不一样,可以同时到一个点(哪怕 ...
- 搜索--P1219 N皇后
题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...
- 【2018百度之星资格赛】 A 问卷调查 - 位运算&动规
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6344 参考博客:在此感谢http://www.cnblogs.com/LQLlulu/p/941923 ...