From 300cd962e572bb0ab3314295721f4d2e5d2d16c7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 2 Feb 2017 14:27:27 +0000 Subject: [PATCH 1/5] 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); From 407bcf1bb932ca53a753ae9d88a21bd44f020710 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 10 Feb 2017 14:22:54 +0000 Subject: [PATCH 2/5] Delete database on logout. DI a SyncAccumulator. Log uncaught errors --- src/Lifecycle.js | 11 +++++++++++ src/MatrixClientPeg.js | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 493bbf12aa..739e8e3832 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -329,10 +329,21 @@ export function startMatrixClient() { */ export function onLoggedOut() { _clearLocalStorage(); + _clearIndexedDB(); stopMatrixClient(); dis.dispatch({action: 'on_logged_out'}); } +function _clearIndexedDB() { + // remove indexeddb instances + if (!window.indexedDB) { + return; + } + console.log("Clearing indexeddb"); + window.indexedDB.deleteDatabase("matrix-js-sdk"); + window.indexedDB.deleteDatabase("logs"); +} + function _clearLocalStorage() { if (!window.localStorage) { return; diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 36521204c5..42834618ae 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -79,6 +79,7 @@ class MatrixClientPeg { console.log("Loading history from IndexedDB."); promise = this.matrixClient.store.startup(); } + promise.catch((err) => { console.error(err); }); promise.finally(() => { this.get().startClient(opts); @@ -124,7 +125,8 @@ class MatrixClientPeg { } if (window.indexedDB && localStorage) { opts.store = new Matrix.IndexedDBStore( - new Matrix.IndexedDBStoreBackend(window.indexedDB), { + new Matrix.IndexedDBStoreBackend(window.indexedDB), + new Matrix.SyncAccumulator(), { localStorage: localStorage, } ); From c46f28213995ef6a10f0c3715bef7b5b43ecc595 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 10 Feb 2017 16:19:39 +0000 Subject: [PATCH 3/5] Comments --- src/MatrixClientPeg.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 42834618ae..0267b36464 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -79,8 +79,11 @@ class MatrixClientPeg { console.log("Loading history from IndexedDB."); promise = this.matrixClient.store.startup(); } + // log any errors when starting up the database 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); }); From f07da44aa55e6076ea28d57b761e627929425c3d Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 16 Feb 2017 12:42:45 +0000 Subject: [PATCH 4/5] Don't handle logs db: It needs to close its connections first --- src/Lifecycle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 9aded55193..87a2878e37 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -360,7 +360,7 @@ function _clearIndexedDB() { } console.log("Clearing indexeddb"); window.indexedDB.deleteDatabase("matrix-js-sdk"); - window.indexedDB.deleteDatabase("logs"); + // TODO: Remove logs db as well. } function _clearLocalStorage() { From cca266c62cea505d8e80e5e32cac07bfccc1caf3 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 17 Feb 2017 10:43:55 +0000 Subject: [PATCH 5/5] Review comments --- src/Lifecycle.js | 12 +----------- src/MatrixClientPeg.js | 9 ++------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index d5683b35a0..295ffe44f4 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -380,21 +380,10 @@ export function startMatrixClient() { */ export function onLoggedOut() { _clearLocalStorage(); - _clearIndexedDB(); stopMatrixClient(); dis.dispatch({action: 'on_logged_out'}); } -function _clearIndexedDB() { - // remove indexeddb instances - if (!window.indexedDB) { - return; - } - console.log("Clearing indexeddb"); - window.indexedDB.deleteDatabase("matrix-js-sdk"); - // TODO: Remove logs db as well. -} - function _clearLocalStorage() { if (!window.localStorage) { return; @@ -423,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 0267b36464..3759aa72c1 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -73,13 +73,8 @@ class MatrixClientPeg { // the react sdk doesn't work without this, so don't allow opts.pendingEventOrdering = "detached"; - 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(); - } - // log any errors when starting up the database + 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