From 6e7b9472be37b50928081d76f1cbcebaa48c2492 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 9 Feb 2017 17:25:40 +0000 Subject: [PATCH] Add a save() method to the store interface Originally I just piggy-backed off setSyncToken() but this was] non-obvious that depending on the store it might do writes to the database. This was exacerbated by the fact that the save needs to be done at the "right time" (after sync data is accumulated and after the sync response has been processed) and setSyncToken was being called too early. A save() method fixes both these things. --- src/store/indexeddb.js | 12 ++++-------- src/store/memory.js | 5 +++++ src/store/stub.js | 5 +++++ src/sync.js | 4 ++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/store/indexeddb.js b/src/store/indexeddb.js index 8fa102eac..30faf40ce 100644 --- a/src/store/indexeddb.js +++ b/src/store/indexeddb.js @@ -284,19 +284,15 @@ IndexedDBStore.prototype.getSyncAccumulator = function() { }; /** - * Set a new sync token and possibly write to the database. - * Overrides MatrixInMemoryStore. - * @param {string} token The new sync token - * @return {?Promise} A promise if this sync token triggered a write to the - * database, else null. Promise resolves after the write completes. + * Possibly write data to the database. + * @return {Promise} Promise resolves after the write completes. */ -IndexedDBStore.prototype.setSyncToken = function(token) { - MatrixInMemoryStore.prototype.setSyncToken.call(this, token); +IndexedDBStore.prototype.save = function() { const now = Date.now(); if (now - this._syncTs > WRITE_DELAY_MS) { return this._syncToDatabase().catch((err) => {console.error("sync fail:", err);}); } - return null; + return q(); }; IndexedDBStore.prototype._setSyncData = function(nextBatch, roomsData) { diff --git a/src/store/memory.js b/src/store/memory.js index cf52f8074..b14b25f7d 100644 --- a/src/store/memory.js +++ b/src/store/memory.js @@ -274,4 +274,9 @@ module.exports.MatrixInMemoryStore.prototype = { getAccountData: function(eventType) { return this.accountData[eventType]; }, + + /** + * Save does nothing as there is no backing data store. + */ + save: function() {}, }; diff --git a/src/store/stub.js b/src/store/stub.js index ad5e3580c..ed9da2ef3 100644 --- a/src/store/stub.js +++ b/src/store/stub.js @@ -179,6 +179,11 @@ StubStore.prototype = { getAccountData: function(eventType) { }, + + /** + * Save does nothing as there is no backing data store. + */ + save: function() {}, }; /** Stub Store class. */ diff --git a/src/sync.js b/src/sync.js index 8931ec92e..c8d0ace77 100644 --- a/src/sync.js +++ b/src/sync.js @@ -570,6 +570,10 @@ SyncApi.prototype._sync = function(syncOptions) { // keep emitting SYNCING -> SYNCING for clients who want to do bulk updates self._updateSyncState("SYNCING", syncEventData); + // tell databases that everything is now in a consistent state and can be + // saved. + client.store.save(); + self._sync(syncOptions); }, function(err) { if (!self._running) {