We want to use IDB to store the wittr messages.

The logic is when the page start:

  1. service worker will read the skeleton from the cache and show to the interface.
  2. read the message data from the IDB first instead of going to network.
  3. Show the data from IDB then open socket to fetch updated wittr.
  4. Once data arrive, update the interface and save into IDB.
export default function IndexController(container) {
this._container = container;
this._postsView = new PostsView(this._container);
this._toastsView = new ToastsView(this._container);
this._lostConnectionToast = null;
this._openSocket();
this._dbPromise = openDatabase();
this._registerServiceWorker();
}

In openDatabase(), we create Wittr db, set id as primary key and time as index called 'by-time'

function openDatabase() {
// If the browser doesn't support service worker,
// we don't care about having a database
if (!navigator.serviceWorker) {
return Promise.resolve();
} return idb.open('wittr', 1, function(upgradeDb){
const store = upgradeDb.createObjectStore('wittrs', {
keyPath: 'id'
}); store.createIndex('by-date', 'time');
});
}

In _openSocket, we have a function to fetch the old data from the IDB.

// open a connection to the server for live updates
IndexController.prototype._openSocket = function() { ... ws.addEventListener('message', function(event) {
requestAnimationFrame(function() {
indexController._onSocketMessage(event.data);
});
}); ...
};
// called when the web socket sends message data
IndexController.prototype._onSocketMessage = function(data) {
var messages = JSON.parse(data); this._dbPromise.then(function(db) {
if (!db) return; // TODO: put each message into the 'wittrs'
// object store.
const tx = db.transaction('wittrs', 'readwrite');
const wittrs = tx.objectStore('wittrs');
messages.forEach( (message) => {
wittrs.put(message);
}); return tx.complete;
}); this._postsView.addPosts(messages);
};

[PWA] 15. Using The IDB Cache And Display Entries的更多相关文章

  1. [PWA] 18. Clean the photo cache

    We cannot let photo always keep caching new data without clean the old data. If message is not displ ...

  2. 第 15 章 可扩展性设计之 Cache 与 Search 的利用

    前言: 前面章节部分所分析的可扩展架构方案,基本上都是围绕在数据库自身来进行的,这样是否会使我们在寻求扩展性之路的思维受到“禁锢”,无法更为宽广的发散开来.这一章,我们就将跳出完全依靠数据库自身来改善 ...

  3. MySQL性能调优与架构设计——第 15 章 可扩展性设计之Cache与Search的利用

    第 15 章 可扩展性设计之Cache与Search的利用 前言: 前面章节部分所分析的可扩展架构方案,基本上都是围绕在数据库自身来进行的,这样是否会使我们在寻求扩展性之路的思维受到“禁锢”,无法更为 ...

  4. cache—主存—辅存三级调度模拟

    近日,在体系结构的课程中,老师留了一个上机作业,为cache—主存—辅存三级调度模拟,要求如下: 实现三级存储体系的模拟调度程序 描述:CPU在cache访问数据块,若命中给出对应的cache实地址, ...

  5. [置顶] 页面缓存,cache,设置缓存过期时间,OutputCache

    页面缓存 方法一: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //缓存有数据 if (Cach ...

  6. google guava cache缓存基本使用讲解

    代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...

  7. 【Guava】Guava Cache用法

    背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...

  8. Design Of A Modern Cache

    http://highscalability.com/blog/2016/1/25/design-of-a-modern-cache.html MONDAY, JANUARY 25, 2016 AT ...

  9. Network Stack‎ : HTTP Cache

    HTTP Cache 目录 1 Operation 2 Sparse Entries 3 Truncated Entries 4 Byte-Range Requests 5 HttpCache::Tr ...

随机推荐

  1. Android 学习手札(一) 应用程序架构

    1.资源列表 Android支持的资源列表 目   录 资源类型 描述 res/anim  XML  该目录用于存放帧(frame).动画或补间(tweened)动画文件 res/drawable   ...

  2. 《用chsh选择shell》-linux命令五分钟系列之十二

    chsh命令用于修改你的登录shell. 1 我想知道我机器安装了哪些shell? 两种方法可以查看: 第一种: [rocrocket@wupengchong ~]$ chsh -l /bin/sh ...

  3. Asp.Net MVC5 格式化输出时间日期

    刚好用到这个,网上找的全部是输出文本框内容的格式化日期时间 而我需要只是在一个表格中的单元个中输出单纯的文字 最后在MSDN上找到 HtmlHelper.FormatValue 方法 public s ...

  4. [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】

    题目链接:BZOJ - 1026 题目分析 这道题是一道数位DP的基础题,对于完全不会数位DP的我来说也是难题.. 对于询问 [a,b] 的区间的答案,我们对询问进行差分,求 [0,b] - [0,a ...

  5. 接受、online、见刊时,期刊的 IF 都不同,究竟算发几分期刊?

  6. MYSQL的binary解决mysql数据大小写敏感问题 《转载》

    BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写如下:mysql> select binary 'ABCD'='abcd ...

  7. IBM Websphere 说明文档

    http://pic.dhe.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Finfo%2F ...

  8. 在InnoDB,记录在 non-clustered indexes(也被称为secondary indexes) 包含了主键值

    In InnoDB, the records in non-clustered indexes (also called secondary indexes) contain the primary ...

  9. 【HDOJ】2266 How Many Equations Can You Find

    简单DFS. #include <cstdio> #include <cstring> #define MAXN 15 char str[MAXN]; __int64 x; i ...

  10. java学习之数组排序一:选择排序

    在讲完java中数组定义的两种方式之外,这里需要讲一下对数组常用的方法,第一个就是排序. 加入我们现在有个数组:int[] arr = {12,87,34,3,98,33,103}; 思路1: 1.首 ...