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. Javascript参数传递中值和引用的一种理解

    值(value)和引用(reference)是各种编程语言老生常谈的话题,js也不例外. 我将剖析一个例子的实际运行过程,跟大家分享我对js参数传递中的值和引用的理解. 参考官网数据类型的两种分类,本 ...

  2. CSS实现DIV三角形

    本文内容收集来自网络 #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px s ...

  3. oe 仓库管理

    需求情景: 销售电商, 其中有些产品 为代理销售(公司不管理库存,建立SO后直接由对应的供应商发货即可) 解决方案: SO 生成 DO 时候 , 源库存的取得逻辑        SO-->SHO ...

  4. 转:11个实用但你可能不知道的Python程序库

    原文来自于:http://www.techug.com/11-python-libraries-you-might-not-know 目前,网上已有成千上万个Python包,但几乎没有人能够全部知道它 ...

  5. 3、MyBatis.Net学习笔记之增删改

    增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...

  6. (摘录)data guard switchover切换异常

    查看DG数据库备份库发现,switchover_status为SWITCHOVER LATENT SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION ...

  7. [BZOJ 2326] [HNOI2011] 数学作业 【矩阵乘法】

    题目链接:BZOJ - 2326 题目分析 数据范围达到了 10^18 ,显然需要矩阵乘法了! 可以发现,向数字尾部添加一个数字 x 的过程就是 Num = Num * 10^k + x .其中 k ...

  8. android如何获取默认的桌面程序

    [方法1] http://stackoverflow.com/questions/12594192/remove-activity-as-default-launcher/12594332#12594 ...

  9. IntegerCache详解

    IntegerCache是Integer的内部类,用来将-128——high之间的对象进行实例化 private static class IntegerCache {        static f ...

  10. build Intent

    Intent用于activity之间, fragmetn之间, 或者APP间通信, 主要包含数据和Action两部分: 常见的action是字符串形式的activity指定 Intent intent ...