1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Make indexeddb startup faster

Don't needlessly transfer data from the worker to the main script
only to immediately transfer it right back again.
This commit is contained in:
David Baker
2017-04-06 18:09:12 +01:00
parent 18806e5524
commit 039a3e258b
4 changed files with 39 additions and 47 deletions

View File

@@ -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 * Clear the entire database. This should be used when logging out of a client
* to prevent mixing data between accounts. * to prevent mixing data between accounts.
@@ -250,7 +268,7 @@ LocalIndexedDBStoreBackend.prototype = {
* sync. * sync.
* @return {Promise<Object[]>} A list of presence events in their raw form. * @return {Promise<Object[]>} A list of presence events in their raw form.
*/ */
loadUserPresenceEvents: function() { getUserPresenceEvents: function() {
return q.try(() => { return q.try(() => {
const txn = this.db.transaction(["users"], "readonly"); const txn = this.db.transaction(["users"], "readonly");
const store = txn.objectStore("users"); const store = txn.objectStore("users");
@@ -264,7 +282,7 @@ LocalIndexedDBStoreBackend.prototype = {
* Load all the account data events from the database. This is not cached. * Load all the account data events from the database. This is not cached.
* @return {Promise<Object[]>} A list of raw global account events. * @return {Promise<Object[]>} A list of raw global account events.
*/ */
loadAccountData: function() { _loadAccountData: function() {
return q.try(() => { return q.try(() => {
const txn = this.db.transaction(["accountData"], "readonly"); const txn = this.db.transaction(["accountData"], "readonly");
const store = txn.objectStore("accountData"); const store = txn.objectStore("accountData");
@@ -278,7 +296,7 @@ LocalIndexedDBStoreBackend.prototype = {
* Load the sync data from the database. * Load the sync data from the database.
* @return {Promise<Object>} An object with "roomsData" and "nextBatch" keys. * @return {Promise<Object>} An object with "roomsData" and "nextBatch" keys.
*/ */
loadSyncData: function() { _loadSyncData: function() {
return q.try(() => { return q.try(() => {
const txn = this.db.transaction(["sync"], "readonly"); const txn = this.db.transaction(["sync"], "readonly");
const store = txn.objectStore("sync"); const store = txn.objectStore("sync");

View File

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

View File

@@ -45,6 +45,9 @@ class IndexedDbStoreWorker {
case 'connect': case 'connect':
prom = this.backend.connect(); prom = this.backend.connect();
break; break;
case 'init':
prom = this.backend.init();
break;
case 'clearDatabase': case 'clearDatabase':
prom = this.backend.clearDatabase().then((result) => { prom = this.backend.clearDatabase().then((result) => {
// This returns special classes which can't be cloned // This returns special classes which can't be cloned
@@ -64,14 +67,8 @@ class IndexedDbStoreWorker {
return {}; return {};
}); });
break; break;
case 'loadUserPresenceEvents': case 'getUserPresenceEvents':
prom = this.backend.loadUserPresenceEvents(); prom = this.backend.getUserPresenceEvents();
break;
case 'loadAccountData':
prom = this.backend.loadAccountData();
break;
case 'loadSyncData':
prom = this.backend.loadSyncData();
break; break;
} }

View File

@@ -111,17 +111,10 @@ IndexedDBStore.prototype.startup = function() {
// the web worker to then immediately push it back again without // the web worker to then immediately push it back again without
// using it. // using it.
return this.backend.connect().then(() => { return this.backend.connect().then(() => {
return q.all([ return this.backend.init();
this.backend.loadUserPresenceEvents(), }).then(() => {
this.backend.loadAccountData(), return this.backend.getUserPresenceEvents();
this.backend.loadSyncData(), }).then((userPresenceEvents) => {
]);
}).then((values) => {
const [userPresenceEvents, accountData, syncData] = values;
console.log(
"Loaded data from database: sync from ", syncData.nextBatch,
" -- Reticulating splines...",
);
userPresenceEvents.forEach(([userId, rawEvent]) => { userPresenceEvents.forEach(([userId, rawEvent]) => {
const u = new User(userId); const u = new User(userId);
if (rawEvent) { if (rawEvent) {
@@ -131,13 +124,6 @@ IndexedDBStore.prototype.startup = function() {
this.storeUser(u); this.storeUser(u);
}); });
this._syncTs = Date.now(); // pretend we've written so we don't rewrite 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,
},
});
}); });
}; };