You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
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.
This commit is contained in:
@@ -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) {
|
||||
|
@@ -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() {},
|
||||
};
|
||||
|
@@ -179,6 +179,11 @@ StubStore.prototype = {
|
||||
getAccountData: function(eventType) {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Save does nothing as there is no backing data store.
|
||||
*/
|
||||
save: function() {},
|
||||
};
|
||||
|
||||
/** Stub Store class. */
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user