Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 573029a

Browse files
dbkrbwindels
authored andcommitted
Fall back to another store if indexeddb start fails
If we can't start indexeddb, fall back to a different store. Previously we just ignored the exception and ploughed on anyway, on the assumption that startup() was just for the indexeddb store to load data anyway, and if that failed it would just do an initial /sync instead (and also we'd keep trying to save the sync back which would fail...). Then, in the previous release we started pulling the settings out of the store on startup, making the assumpton that the store actually worked, so the read obviously failed and the app failed to start up. This makes Riot work in Tor browser / firefox in daft mode again.
1 parent fd64369 commit 573029a

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/MatrixClientPeg.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ limitations under the License.
1818

1919
'use strict';
2020

21+
import Matrix from 'matrix-js-sdk';
22+
2123
import utils from 'matrix-js-sdk/lib/utils';
2224
import EventTimeline from 'matrix-js-sdk/lib/models/event-timeline';
2325
import EventTimelineSet from 'matrix-js-sdk/lib/models/event-timeline-set';
@@ -51,6 +53,9 @@ class MatrixClientPeg {
5153
this.opts = {
5254
initialSyncLimit: 20,
5355
};
56+
// the credentials used to init the current client object.
57+
// used if we tear it down & recreate it with a different store
58+
this._currentClientCreds = null;
5459
}
5560

5661
/**
@@ -79,10 +84,30 @@ class MatrixClientPeg {
7984
* Home Server / Identity Server URLs and active credentials
8085
*/
8186
replaceUsingCreds(creds: MatrixClientCreds) {
87+
this._currentClientCreds = creds;
8288
this._createClient(creds);
8389
}
8490

8591
async start() {
92+
for (const dbType of ['indexeddb', 'memory']) {
93+
try {
94+
const promise = this.matrixClient.store.startup();
95+
console.log("MatrixClientPeg: waiting for MatrixClient store to initialise");
96+
await promise;
97+
break;
98+
} catch (err) {
99+
if (dbType === 'indexeddb') {
100+
console.error('Error starting matrixclient store - falling back to memory store', err);
101+
this.matrixClient.store = new Matrix.MatrixInMemoryStore({
102+
localStorage: global.localStorage,
103+
});
104+
} else {
105+
console.error('Failed to start memory store!', err);
106+
throw err;
107+
}
108+
}
109+
}
110+
86111
// try to initialise e2e on the new client
87112
try {
88113
// check that we have a version of the js-sdk which includes initCrypto
@@ -103,18 +128,6 @@ class MatrixClientPeg {
103128
opts.lazyLoadMembers = true;
104129
}
105130

106-
try {
107-
const promise = this.matrixClient.store.startup();
108-
console.log(`MatrixClientPeg: waiting for MatrixClient store to initialise`);
109-
await promise;
110-
} catch (err) {
111-
// log any errors when starting up the database (if one exists)
112-
console.error('Error starting matrixclient store', err);
113-
}
114-
115-
// regardless of errors, start the client. If we did error out, we'll
116-
// just end up doing a full initial /sync.
117-
118131
// Connect the matrix client to the dispatcher
119132
MatrixActionCreators.start(this.matrixClient);
120133

@@ -147,7 +160,7 @@ class MatrixClientPeg {
147160
return matches[1];
148161
}
149162

150-
_createClient(creds: MatrixClientCreds) {
163+
_createClient(creds: MatrixClientCreds, useIndexedDb) {
151164
const opts = {
152165
baseUrl: creds.homeserverUrl,
153166
idBaseUrl: creds.identityServerUrl,
@@ -158,7 +171,7 @@ class MatrixClientPeg {
158171
forceTURN: SettingsStore.getValue('webRtcForceTURN', false),
159172
};
160173

161-
this.matrixClient = createMatrixClient(opts, this.indexedDbWorkerScript);
174+
this.matrixClient = createMatrixClient(opts, useIndexedDb);
162175

163176
// we're going to add eventlisteners for each matrix event tile, so the
164177
// potential number of event listeners is quite high.

src/utils/createMatrixClient.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ try {
3232
* @param {Object} opts options to pass to Matrix.createClient. This will be
3333
* extended with `sessionStore` and `store` members.
3434
*
35+
* @param {bool} useIndexedDb True to attempt to use indexeddb, or false to force
36+
* use of the memory store. Default: true.
37+
*
3538
* @property {string} indexedDbWorkerScript Optional URL for a web worker script
3639
* for IndexedDB store operations. By default, indexeddb ops are done on
3740
* the main thread.
3841
*
3942
* @returns {MatrixClient} the newly-created MatrixClient
4043
*/
41-
export default function createMatrixClient(opts) {
44+
export default function createMatrixClient(opts, useIndexedDb) {
45+
if (useIndexedDb === undefined) useIndexedDb = true;
46+
4247
const storeOpts = {
4348
useAuthorizationHeader: true,
4449
};
@@ -47,7 +52,7 @@ export default function createMatrixClient(opts) {
4752
storeOpts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
4853
}
4954

50-
if (indexedDB && localStorage) {
55+
if (indexedDB && localStorage && useIndexedDb) {
5156
storeOpts.store = new Matrix.IndexedDBStore({
5257
indexedDB: indexedDB,
5358
dbName: "riot-web-sync",

0 commit comments

Comments
 (0)