You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
Make indexeddb save after the first sync
Save the sync data to indexeddb after the first catch-up Fixes https://github.com/vector-im/riot-web/issues/3527
This commit is contained in:
@@ -89,7 +89,7 @@ const IndexedDBStore = function IndexedDBStore(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.startedUp = false;
|
this.startedUp = false;
|
||||||
this._syncTs = Date.now(); // updated when writes to the database are performed
|
this._syncTs = 0;
|
||||||
|
|
||||||
// Records the last-modified-time of each user at the last point we saved
|
// Records the last-modified-time of each user at the last point we saved
|
||||||
// the database, such that we can derive the set if users that have been
|
// the database, such that we can derive the set if users that have been
|
||||||
@@ -123,7 +123,6 @@ IndexedDBStore.prototype.startup = function() {
|
|||||||
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
|
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
|
||||||
this.storeUser(u);
|
this.storeUser(u);
|
||||||
});
|
});
|
||||||
this._syncTs = Date.now(); // pretend we've written so we don't rewrite
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -157,28 +156,32 @@ IndexedDBStore.prototype.deleteAllData = function() {
|
|||||||
IndexedDBStore.prototype.save = function() {
|
IndexedDBStore.prototype.save = function() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (now - this._syncTs > WRITE_DELAY_MS) {
|
if (now - this._syncTs > WRITE_DELAY_MS) {
|
||||||
this._syncTs = Date.now(); // set now to guard against multi-writes
|
return this._reallySave();
|
||||||
|
|
||||||
// work out changed users (this doesn't handle deletions but you
|
|
||||||
// can't 'delete' users as they are just presence events).
|
|
||||||
const userTuples = [];
|
|
||||||
for (const u of this.getUsers()) {
|
|
||||||
if (this._userModifiedMap[u.userId] === u.getLastModifiedTime()) continue;
|
|
||||||
if (!u.events.presence) continue;
|
|
||||||
|
|
||||||
userTuples.push([u.userId, u.events.presence.event]);
|
|
||||||
|
|
||||||
// note that we've saved this version of the user
|
|
||||||
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.backend.syncToDatabase(userTuples).catch((err) => {
|
|
||||||
console.error("sync fail:", err);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return q();
|
return q();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
IndexedDBStore.prototype._reallySave = function() {
|
||||||
|
this._syncTs = Date.now(); // set now to guard against multi-writes
|
||||||
|
|
||||||
|
// work out changed users (this doesn't handle deletions but you
|
||||||
|
// can't 'delete' users as they are just presence events).
|
||||||
|
const userTuples = [];
|
||||||
|
for (const u of this.getUsers()) {
|
||||||
|
if (this._userModifiedMap[u.userId] === u.getLastModifiedTime()) continue;
|
||||||
|
if (!u.events.presence) continue;
|
||||||
|
|
||||||
|
userTuples.push([u.userId, u.events.presence.event]);
|
||||||
|
|
||||||
|
// note that we've saved this version of the user
|
||||||
|
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.backend.syncToDatabase(userTuples).catch((err) => {
|
||||||
|
console.error("sync fail:", err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
IndexedDBStore.prototype.setSyncData = function(syncData) {
|
IndexedDBStore.prototype.setSyncData = function(syncData) {
|
||||||
return this.backend.setSyncData(syncData);
|
return this.backend.setSyncData(syncData);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -620,11 +620,13 @@ SyncApi.prototype._sync = function(syncOptions) {
|
|||||||
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
|
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
|
||||||
if (!isCachedResponse) {
|
if (!isCachedResponse) {
|
||||||
self._updateSyncState("SYNCING", syncEventData);
|
self._updateSyncState("SYNCING", syncEventData);
|
||||||
|
|
||||||
|
// tell databases that everything is now in a consistent state and can be
|
||||||
|
// saved (no point doing so if we only have the data we just got out of the
|
||||||
|
// store).
|
||||||
|
client.store.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
// tell databases that everything is now in a consistent state and can be
|
|
||||||
// saved.
|
|
||||||
client.store.save();
|
|
||||||
|
|
||||||
// Begin next sync
|
// Begin next sync
|
||||||
self._sync(syncOptions);
|
self._sync(syncOptions);
|
||||||
|
|||||||
Reference in New Issue
Block a user