[Vue-rx] Cache Remote Data Requests with RxJS and Vue.js
A Promise invokes a function which stores a value that will be passed to a callback. So when you wrap a Promise with an Observable, you'll always get that same value. This enables you to use the behavior as a caching mechanism when the Promises make requests for remote data.
const p = new Promise((resolve, reject )=> {
console.log("Promise started"); // This line will be print out only once, when the promise was invoked
resolve(new Date());
});
// The output date should be the same, since promise was only invoke once
p.then(( date)=> {
console.log(date) // Thu Jul 19 2018 12:55:41 GMT+0300 (EEST)
})
setTimeout(( )=> {
p.then(( date)=> {
console.log(date) //Thu Jul 19 2018 12:55:41 GMT+0300 (EEST)
})
}, 2000);
Caching data in RxJS can be as simple as creating a caching function which can store the values in an object. This lessons walks through creating a caching function and explains how the function closes over an object then pairs a url with an Observable that returns the resolution of a Promise
let cache = {};
// Cache function
const cachePerson = cache => url =>
cache[url] ?
cache[url]:
cache[url] = createLoader(url);
const activeTab$ = this.$watchAsObservable('activeTab', {
immediate: true
}).pipe(pluck('newValue'));
// this.$http.get() return a promise, then convert to Observable using from()
const createLoader = url => from(this.$http.get(url)).pipe(pluck('data'));
const people$ = createLoader('https://starwars.egghead.training/people').pipe(
map(people => people.slice(0,7 ))
);
const person$ = combineLatest(
activeTab$,
people$,
(people$, (tabId, people) => people[tabId].id))
.pipe(
map(id => `https://starwars.egghead.training/people/${id}`),
switchMap(cachePerson(cache)),
catchError(() => of({ name: 'Failed.. :(' })),
share()
);
[Vue-rx] Cache Remote Data Requests with RxJS and Vue.js的更多相关文章
- vue单文件组件data选项的函数体获取vue实例对象
因配置的关系,导致 vue的data选项中存在事件.而事件无法获取 vue 的实例对象:项目是单文件形式的,以下代码只是例子 new Vue({ el:..., data:{ a: { onevent ...
- [GraphQL] Query Local and Remote Data in Apollo Link State
In this lesson, you will learn how to query local and remote data in Apollo Link State in the same c ...
- Angular Multiple HTTP Requests with RxJS
原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...
- Knockout Grid - Loading Remote Data
http://wijmo.com/grid-with-knockout-viewmodel-loading-remote-data/ We were hearing quite a few peopl ...
- fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)
华为G6-C00卡刷提示OEMSBL错误,只能线刷 ,但是官方找不到线刷img镜像,无奈 网上下了个可以线刷的工具套件 流氓ROM . 使用HuaweiUpdateExtractor(工具百度)把官方 ...
- 【Vue】定义组件 data 必须是一个函数返回的对象
Vue 实例的数据对象.Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化.对象必须是纯粹的对象 (含有零个或多个的 key/value ...
- Vue实例的的data对象
介绍 Vue的实例的数据对象data 我们已经用了很多了,数据绑定离不开data里面的数据.也是Vue的核心属性. 它是Vue绑定数据到HTML标签的数据源泉,另外Vue框架会自动监视data里面的数 ...
- 15.Vue组件中的data
1.组件中展示数据和响应事件: // 1. 组件可以有自己的 data 数据 // 2. 组件的 data 和 实例的 data 有点不一样,实例中的 data 可以为一个对象 // 3. 但是组件中 ...
- Vue中用props给data赋初始值遇到的问题解决
Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14 作者:yuyongyu 我要评论 这篇文章主要介绍了Vue中用props给dat ...
随机推荐
- [转]iOS WebKit browsers and auto-zooming form controls
问题描述:https://github.com/jquery/jquery-mobile/issues/2581 本文转自:http://www.456bereastreet.com/archive/ ...
- 轮播图-version1
实现目标 按'>'出现下一caption,按'<'出现上一caption 按下面的点到指定的caption 自动轮播 思路: 设置一个carousel容器,里面有carousel的每一张图 ...
- (2)左右值初探与auto类型说明符
这篇文章的起因是下面这两段代码,出自<C++ primer 5th>中文版P62页: auto &h =42;//错误,不能为非常量引用绑定字面值 const auto & ...
- Spartan6系列之SelectIO深入详解及高级应用简介
1. 什么是I/O Tile? 对Spartan-6系列FPGA来说,一个IO Tile包括2个IOB.2个ILOGIC.2个OLOGIC.2个IODELAY. 图 1Spartan-6系列 ...
- js统计图表插件 Echarts
Echarts 用于制作数据统计图表,一个纯 Javascript 的图表库,快捷简便的生成统计图表. 官网:https://www.echartsjs.com/ 效果 html <!DOCTY ...
- Angular ZoneJS 原理
Zone.js到底是如何工作的? 原文链接: blog.kwintenp.com 如果你阅读过关于Angular 2变化检测的资料,那么你很可能听说过zone.Zone是一个从Dart中引入的特性并被 ...
- 03Struts2基本使用流程
Struts2基本使用流程 1.新建web工程 2.引入struts2类库 3.创建并配置Struts2的核心控制器web.xml用来拦截客户端请求并将请求转发到相应的Action类中来处理 4.创建 ...
- uploadify的简单使用
简单的图片上传: 1.进入官网下载uploadify插件:http://www.uploadify.com/download/ 2.导入uploadify插件提供的css样式和类库: <link ...
- C/C++格式化输入,输出
C/C++格式化输入,输出 1.C语言 1. 语言函数 scanf(); printf(); sscanf() --> 不安全 sscanf_s() ---> 安全 sprintf() - ...
- jsp页面应用简记
1.记录总数据条数 <div class="" > <span class="">共有数据:<strong>${fn:len ...