From 300cd962e572bb0ab3314295721f4d2e5d2d16c7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 2 Feb 2017 14:27:27 +0000 Subject: [PATCH] Add glue code to make react-sdk use indexedDB --- src/MatrixClientPeg.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 9c0daf4726..36521204c5 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,17 @@ 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 = q(); + if (this.matrixClient.store instanceof Matrix.IndexedDBStore) { + // load from storage before starting up. + console.log("Loading history from IndexedDB."); + promise = this.matrixClient.store.startup(); + } + + promise.finally(() => { + this.get().startClient(opts); + }); } getCredentials(): MatrixClientCreds { @@ -111,6 +122,13 @@ class MatrixClientPeg { if (localStorage) { opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage); } + if (window.indexedDB && localStorage) { + opts.store = new Matrix.IndexedDBStore( + new Matrix.IndexedDBStoreBackend(window.indexedDB), { + localStorage: localStorage, + } + ); + } this.matrixClient = Matrix.createClient(opts);