[RxJS] Reactive Programming - Sharing network requests with shareReplay()
2016-03-09 03:31
591 查看
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:
We actually can use the same network request by 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.
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 - Clear data while loading with RxJS startWith()
- windows7搭建react native开发环境
- ReactiveCocoa 学习心得 -- 1
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
- MVVM-ReactiveCocoa
- 【无源汇有上下界可行流】ACdream1211-Reactor Cooling
- react与native异步交互的几种方式
- ACE反应器(Reactor)模式(4)
- ACE反应器(Reactor)模式(3)
- ACE反应器(Reactor)模式(2)
- ACE反应器(Reactor)模式(1)
- React Native(第二节点击事件)
- React Native开发之按钮的使用
- ReactiveCocoa入门教程——第二部分
- ReactiveCocoa入门教程——第一部分
- React-约束组件及无约束组件详解
- 源码推荐(03.08):ReactiveCocoa登录交互效果的实现,可编辑可拖动排序
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
- [RxJS] Reactive Programming - Why choose RxJS?
- React模板