[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 ...
随机推荐
- 在Debian Wheezy 7.3.0上编译安装3.12.14内核
最近需要对Linux的一个内核模块进行调整实验,故决定先在虚拟机中完成编译调试工作,最后再在真实的系统上进行测试.为了防止遗忘,把过程记录于此. 1. 准备系统环境 首先从官网下载最新版的Virtua ...
- MODULE_DEVICE_TABLE
1. MODULE_DEVICE_TABLE (usb, skel_table);该宏生成一个名为__mod_pci_device_table的局部变量,该变量指向第二个参数.内核构建时,depmod ...
- 利用ESLint检查代码质量
1. ESLint ESLint 是一个插件化的 javascript 代码检测工具,它可以用于检查常见的 JavaScript 代码错误,也可以进行代码风格检查,这样我们就可以根据自己的喜好指定一套 ...
- How to get multi-touch working(Linux and Andriod)
1.在hid-ids.h中加入vid pid 2.在hid-multitouch..c->mt_devices[] 中加入 { ...
- Python顺序与range和random
range([start,] stop[, step]) start是开始,stop是停下,step是步长. >>> range(10) range(0, 10) >>& ...
- 动软代码生成V2.74模版简介
最近发现很多人用动软代码生成,确实方便,有些经验记录下,以后查看回顾. ..\Maticsoft\Codematic2\Template\TemplateFile 为模板文件夹,直接在目录下新建文件夹 ...
- Cretiria查询应用(二)
1.条件查询,动态查询 public void conditionQuery(){ Session session=null; try { session=HibernateUtil.currentS ...
- python 登陆一个网站
今天想用python写一个登陆的脚本,搜了一下,网上挺多的,看了一些后写了个登陆虎扑论坛的脚本. 原理: 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解coo ...
- C++ 知识点1
typedef的陷阱 严格来说typedef并不是定义别名,而是定义类型,比如typedef int a;按照大部分书本说来,就是把a看做int,这种说法初学看来是正确的,也易于理解,但是遇到type ...
- iOS学习之导航条NavigationControl的一些属性设置
/** * 配置公共的属性,该属性作用于所有的导航条界面; */ - (void)configureConmmonPropety { //1.设置导航条的颜色 self.navigationContr ...