zoukankan      html  css  js  c++  java
  • [PWA] 16. Clean IDB

    When we save items to IndexDB, we need to think about clean the items or keep those in a short list.

    IndexController.prototype._onSocketMessage = function(data) {
      var messages = JSON.parse(data);
    
      this._dbPromise.then(function(db) {
        if (!db) return;
    
        var tx = db.transaction('wittrs', 'readwrite');
        var store = tx.objectStore('wittrs');
        messages.forEach(function(message) {
          store.put(message);
        });
    
        // TODO: keep the newest 30 entries in 'wittrs',
        // but delete the rest.
        //
        // Hint: you can use .openCursor(null, 'prev') to
        // open a cursor that goes through an index/store
        // backwards.
        store.index('by-date').openCursor(null, 'prev')
            .then((cursor)=>{
              return cursor.advance(30); // only 30 itmes are kept
            }).then(function deleteCursor(cursor){
                if(!cursor){
                  return;
                }else{
                  cursor.delete();
                  return cursor.continue().then(deleteCursor);
                }
            })
      });
    
      this._postsView.addPosts(messages);
    };
  • 相关阅读:
    数据结构学习
    古兰查询 之查询页面隐藏
    Qt只QSetting
    学习下知然网友写的taskqueue
    producter-consumer 他山之石
    unix缓冲
    Buffering of C streams
    POCO Log库
    linux下open和fopen的区别
    dup2替换
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5515785.html
Copyright © 2011-2022 走看看