Skip to content

Commit

Permalink
Merge pull request #414 from matrix-org/dbkr/indexeddb_save_after_fir…
Browse files Browse the repository at this point in the history
…st_sync

Make indexeddb save after the first sync
  • Loading branch information
dbkr authored Apr 7, 2017
2 parents 6df4a36 + ce9e69c commit 5a764bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
37 changes: 20 additions & 17 deletions src/store/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const IndexedDBStore = function IndexedDBStore(opts) {
}

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
// the database, such that we can derive the set if users that have been
Expand Down Expand Up @@ -119,7 +119,6 @@ IndexedDBStore.prototype.startup = function() {
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
this.storeUser(u);
});
this._syncTs = Date.now(); // pretend we've written so we don't rewrite
});
};

Expand Down Expand Up @@ -153,26 +152,30 @@ IndexedDBStore.prototype.deleteAllData = function() {
IndexedDBStore.prototype.save = function() {
const now = Date.now();
if (now - this._syncTs > WRITE_DELAY_MS) {
this._syncTs = Date.now(); // set now to guard against multi-writes
return this._reallySave();
}
return q();
};

// 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;
IndexedDBStore.prototype._reallySave = function() {
this._syncTs = Date.now(); // set now to guard against multi-writes

userTuples.push([u.userId, u.events.presence.event]);
// 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;

// note that we've saved this version of the user
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
}
userTuples.push([u.userId, u.events.presence.event]);

return this.backend.syncToDatabase(userTuples).catch((err) => {
console.error("sync fail:", err);
});
// note that we've saved this version of the user
this._userModifiedMap[u.userId] = u.getLastModifiedTime();
}
return q();

return this.backend.syncToDatabase(userTuples).catch((err) => {
console.error("sync fail:", err);
});
};

IndexedDBStore.prototype.setSyncData = function(syncData) {
Expand Down
8 changes: 5 additions & 3 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,13 @@ SyncApi.prototype._sync = function(syncOptions) {
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
if (!isCachedResponse) {
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
self._sync(syncOptions);
Expand Down

0 comments on commit 5a764bb

Please sign in to comment.