diff --git a/src/Lifecycle.js b/src/Lifecycle.js index e891ab5984..295ffe44f4 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -412,6 +412,7 @@ export function stopMatrixClient() { if (cli) { cli.stopClient(); cli.removeAllListeners(); + cli.store.deleteAllData(); MatrixClientPeg.unset(); } } diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 9c0daf4726..3759aa72c1 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -16,6 +16,7 @@ limitations under the License. 'use strict'; +import q from "q"; import Matrix from 'matrix-js-sdk'; import utils from 'matrix-js-sdk/lib/utils'; import EventTimeline from 'matrix-js-sdk/lib/models/event-timeline'; @@ -71,7 +72,16 @@ class MatrixClientPeg { const opts = utils.deepCopy(this.opts); // the react sdk doesn't work without this, so don't allow opts.pendingEventOrdering = "detached"; - this.get().startClient(opts); + + let promise = this.matrixClient.store.startup(); + // log any errors when starting up the database (if one exists) + promise.catch((err) => { console.error(err); }); + + // regardless of errors, start the client. If we did error out, we'll + // just end up doing a full initial /sync. + promise.finally(() => { + this.get().startClient(opts); + }); } getCredentials(): MatrixClientCreds { @@ -111,6 +121,14 @@ class MatrixClientPeg { if (localStorage) { opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage); } + if (window.indexedDB && localStorage) { + opts.store = new Matrix.IndexedDBStore( + new Matrix.IndexedDBStoreBackend(window.indexedDB), + new Matrix.SyncAccumulator(), { + localStorage: localStorage, + } + ); + } this.matrixClient = Matrix.createClient(opts);