Skip to content

Commit

Permalink
Make indexeddb startup faster
Browse files Browse the repository at this point in the history
Don't needlessly transfer data from the worker to the main script
only to immediately transfer it right back again.
  • Loading branch information
dbkr committed Apr 6, 2017
1 parent 18806e5 commit 039a3e2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
24 changes: 21 additions & 3 deletions src/store/indexeddb-local-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ LocalIndexedDBStoreBackend.prototype = {
});
},

/**
* Having connected, load initial data from the database and prepare for use
*/
init: function() {
return q.all([
this._loadAccountData(),
this._loadSyncData(),
]).then(([accountData, syncData]) => {
this._syncAccumulator.accumulate({
next_batch: syncData.nextBatch,
rooms: syncData.roomsData,
account_data: {
events: accountData,
},
});
});
},

/**
* Clear the entire database. This should be used when logging out of a client
* to prevent mixing data between accounts.
Expand Down Expand Up @@ -250,7 +268,7 @@ LocalIndexedDBStoreBackend.prototype = {
* sync.
* @return {Promise<Object[]>} A list of presence events in their raw form.
*/
loadUserPresenceEvents: function() {
getUserPresenceEvents: function() {
return q.try(() => {
const txn = this.db.transaction(["users"], "readonly");
const store = txn.objectStore("users");
Expand All @@ -264,7 +282,7 @@ LocalIndexedDBStoreBackend.prototype = {
* Load all the account data events from the database. This is not cached.
* @return {Promise<Object[]>} A list of raw global account events.
*/
loadAccountData: function() {
_loadAccountData: function() {
return q.try(() => {
const txn = this.db.transaction(["accountData"], "readonly");
const store = txn.objectStore("accountData");
Expand All @@ -278,7 +296,7 @@ LocalIndexedDBStoreBackend.prototype = {
* Load the sync data from the database.
* @return {Promise<Object>} An object with "roomsData" and "nextBatch" keys.
*/
loadSyncData: function() {
_loadSyncData: function() {
return q.try(() => {
const txn = this.db.transaction(["sync"], "readonly");
const store = txn.objectStore("sync");
Expand Down
27 changes: 9 additions & 18 deletions src/store/indexeddb-remote-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ RemoteIndexedDBStoreBackend.prototype = {
return this._doCmd('connect');
},

/**
* Having connected, load initial data from the database and prepare for use
*/
init: function() {
return this._doCmd('init');
},

/**
* Clear the entire database. This should be used when logging out of a client
* to prevent mixing data between accounts.
Expand Down Expand Up @@ -86,24 +93,8 @@ RemoteIndexedDBStoreBackend.prototype = {
* Load all user presence events from the database. This is not cached.
* @return {Promise<Object[]>} A list of presence events in their raw form.
*/
loadUserPresenceEvents: function() {
return this._doCmd('loadUserPresenceEvents');
},

/**
* Load all the account data events from the database. This is not cached.
* @return {Promise<Object[]>} A list of raw global account events.
*/
loadAccountData: function() {
return this._doCmd('loadAccountData');
},

/**
* Load the sync data from the database.
* @return {Promise<Object>} An object with "roomsData" and "nextBatch" keys.
*/
loadSyncData: function() {
return this._doCmd('loadSyncData');
getUserPresenceEvents: function() {
return this._doCmd('getUserPresenceEvents');
},

_doCmd: function(cmd, args) {
Expand Down
13 changes: 5 additions & 8 deletions src/store/indexeddb-remote-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class IndexedDbStoreWorker {
case 'connect':
prom = this.backend.connect();
break;
case 'init':
prom = this.backend.init();
break;
case 'clearDatabase':
prom = this.backend.clearDatabase().then((result) => {
// This returns special classes which can't be cloned
Expand All @@ -64,14 +67,8 @@ class IndexedDbStoreWorker {
return {};
});
break;
case 'loadUserPresenceEvents':
prom = this.backend.loadUserPresenceEvents();
break;
case 'loadAccountData':
prom = this.backend.loadAccountData();
break;
case 'loadSyncData':
prom = this.backend.loadSyncData();
case 'getUserPresenceEvents':
prom = this.backend.getUserPresenceEvents();
break;
}

Expand Down
22 changes: 4 additions & 18 deletions src/store/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,10 @@ IndexedDBStore.prototype.startup = function() {
// the web worker to then immediately push it back again without
// using it.
return this.backend.connect().then(() => {
return q.all([
this.backend.loadUserPresenceEvents(),
this.backend.loadAccountData(),
this.backend.loadSyncData(),
]);
}).then((values) => {
const [userPresenceEvents, accountData, syncData] = values;
console.log(
"Loaded data from database: sync from ", syncData.nextBatch,
" -- Reticulating splines...",
);
return this.backend.init();
}).then(() => {
return this.backend.getUserPresenceEvents();
}).then((userPresenceEvents) => {
userPresenceEvents.forEach(([userId, rawEvent]) => {
const u = new User(userId);
if (rawEvent) {
Expand All @@ -131,13 +124,6 @@ IndexedDBStore.prototype.startup = function() {
this.storeUser(u);
});
this._syncTs = Date.now(); // pretend we've written so we don't rewrite
return this.setSyncData({
next_batch: syncData.nextBatch,
rooms: syncData.roomsData,
account_data: {
events: accountData,
},
});
});
};

Expand Down

0 comments on commit 039a3e2

Please sign in to comment.