From 0d4833d6e33869b82282e29e16d8273b529e0af6 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 10 Feb 2017 14:27:56 +0000 Subject: [PATCH] Make it all work! --- src/store/indexeddb.js | 34 +++++++++++++++++++--------------- src/sync-accumulator.js | 1 + 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/store/indexeddb.js b/src/store/indexeddb.js index 30faf40ce..edaff6c10 100644 --- a/src/store/indexeddb.js +++ b/src/store/indexeddb.js @@ -61,8 +61,15 @@ IndexedDBStoreBackend.prototype = { } // Expand as needed. }; + return promiseifyRequest(req).then((ev) => { this.db = ev.target.result; + + // add a poorly-named listener for when deleteDatabase is called + // so we can close our db connections. + this.db.onversionchange = () => { + this.db.close(); + }; }); }, @@ -87,6 +94,7 @@ IndexedDBStoreBackend.prototype = { nextBatch: nextBatch, roomsData: roomsData, }; + console.log("persisting sync data"); return this._upsert("sync", [obj]); }, @@ -114,7 +122,6 @@ IndexedDBStoreBackend.prototype = { * @return {Promise} Resolves if the users were persisted. */ persistUsers: function(users) { - console.log("persistUsers =>", users); return this._upsert("users", users); }, @@ -148,13 +155,14 @@ IndexedDBStoreBackend.prototype = { return q.try(() => { const txn = this.db.transaction(["sync"], "readonly"); const store = txn.objectStore("sync"); - const results = selectQuery(store, undefined, (cursor) => { + return selectQuery(store, undefined, (cursor) => { return cursor.value; + }).then((results) => { + if (results.length > 1) { + console.warn("loadSyncData: More than 1 sync row found."); + } + return (results.length > 0 ? results[0] : {}); }); - if (results.length > 1) { - console.warn("loadSyncData: More than 1 sync row found."); - } - return (results.length > 0 ? results[0] : {}); }); }, @@ -251,26 +259,22 @@ IndexedDBStore.prototype.startup = function() { return q.all([ this.backend.loadUsers(), this.backend.loadAccountData(), - this.backend.loadRooms(), this.backend.loadSyncData(), ]); }).then((values) => { - const [users, accountData, rooms, syncData] = values; + const [users, accountData, syncData] = values; console.log( - "Loaded data from database. Reticulating splines...", - accountData, users, + "Loaded data from database: sync from ", syncData, + " -- Reticulating splines...", ); users.forEach((u) => { this._userModifiedMap[u.userId] = u.getLastModifiedTime(); this.storeUser(u); }); this.storeAccountDataEvents(accountData); - rooms.forEach((r) => { - this.storeRoom(r); - }); this._syncTs = Date.now(); // pretend we've written so we don't rewrite - this.setSyncToken(syncData.syncToken); - this._setSyncData(syncData.syncToken, syncData.roomsData); + this.setSyncToken(syncData.nextBatch); + this._setSyncData(syncData.nextBatch, syncData.roomsData); }); }; diff --git a/src/sync-accumulator.js b/src/sync-accumulator.js index 65df31e07..344f448aa 100644 --- a/src/sync-accumulator.js +++ b/src/sync-accumulator.js @@ -75,6 +75,7 @@ class SyncAccumulator { if (!syncResponse.rooms) { return; } + console.log("Accumulating ", syncResponse.next_batch, syncResponse); this.nextBatch = syncResponse.next_batch; if (syncResponse.rooms.invite) { Object.keys(syncResponse.rooms.invite).forEach((roomId) => {