[RxJS] Reactive Programming - Sharing network requests with shareReplay()
Currently we show three users in the list, it actually do three time network request, we can verfiy this by console out each network request:
var responseStream = startupRequestStream.merge(requestOnRefreshStream)
.flatMap(requestUrl => {
console.log('do network request');
return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
});
We actually can use the same network request by shareReplay(1):
var responseStream = startupRequestStream.merge(requestOnRefreshStream)
.flatMap(requestUrl => {
console.log('do network request');
return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
})
.shareReplay(1);
Why replay one? Well, that's because if there happens to be a really late subscriber...let's say someone does a setTimeout and doesn't subscribe to the responseStream after a long while. Let's say, three seconds or even 10 seconds. Then, this subscribe will get a replayed response JSON. It will not do a new network request, but it will simply replay that same JSON that happened before.
[RxJS] Reactive Programming - Sharing network requests with shareReplay()的更多相关文章
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the ...
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
In currently implemention, there is one problem, when the page load and click refresh button, the us ...
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [RxJS] Reactive Programming - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...
- [Reactive Programming] Async requests and responses in RxJS
We will learn how to perform network requests to a backend using RxJS Observables. A example of basi ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
- "Principles of Reactive Programming" 之<Actors are Distributed> (1)
week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Clus ...
随机推荐
- 手机软件记事本(SuperNotepad)的使用教程
软件简介: 手机应用记事本(SuperNotepad)类似电脑应用notepad, 可用于文本阅读和编辑新建电子书(本应用限文本txt文件),是阅读小说和便签记录的好帮手. 电子书阅读器及便签的手机应 ...
- Java基础知识强化64:基本类型包装类的引入
1. 基本类型包装类概述 (1)将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据. (2)常用的操作的之一:用于基本数据类型与字符串之间的转换. (3)基本类型和包装类的对应 ...
- C#位移运算符
代码如下: /// <summary> /// 位移运算符"<<"左位移运算符,">>"右位移运算符 /// 在进行位移运算 ...
- Combo( 自定义下拉框) 组件
本节课重点了解 EasyUI 中 Combo(自定义下拉框)组件的使用方法,这个组件依赖于ValidateBox(验证框)组件 一. 加载方式自定义下拉框不能通过标签的方式进行创建.<input ...
- WPF样式和资源2
<Window.Resources> <FontFamily x:key="ButtonFontFamily">Time New Roman</Fon ...
- (转)url重写
使用URLRewriter.dll后,根本不需要使用任何代码,我之前做的项目就是用的做URL重写的,其实不是进化,其实表面上看是.html扩展名而已,当然你还可以用其他的任意扩展名下面是你的配置 &l ...
- Width vs Pitch
1.单位不同,width是像素,pitch是字节.因此一个640*480的8位图和640*480的32位 图他们width一样而pitch不一样. 2.pitch可能大于width个像素所占字节数.w ...
- 解决JavaScript中如何输出空格
在写JS代码的时候,大家可以会发现这样现象:document.write(" 1 2 3 ");结果: 1 2 3无论在输出的内容中什 ...
- php 之 类,对象
--恢复内容结束--- 一.类和对象: 1.定义: 对象:我们所见到的东西都可以称之为对象,是类实例化出来的东西 类:是对所有的同类对象抽象出来的东西 eg: 在一张表中记录了全班同学的学号,姓名,性 ...
- 对mysql进行分表
1. 有某个自段进行md5散列,然后生成ord SCII码 $num = ord(md5($user)) //是一个数字 参考 $num/3 ,$num/4;如果我们不是严格意义上的分表,可以参考分布 ...