From 2aa0fa592c57c453920eb0244764b83f63b1bb66 Mon Sep 17 00:00:00 2001 From: thecodrr Date: Sat, 19 Sep 2020 11:33:31 +0500 Subject: [PATCH] fix: properly clear data from memory on logout --- packages/core/api/vault.js | 3 +++ packages/core/collections/collection.js | 5 ++++- packages/core/database/cached-collection.js | 3 ++- packages/core/database/indexed-collection.js | 6 +++++- packages/core/database/indexer.js | 5 +++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js index 524adddae..783c6ea49 100644 --- a/packages/core/api/vault.js +++ b/packages/core/api/vault.js @@ -15,6 +15,9 @@ export default class Vault { vaultLocked: "ERR_VAULT_LOCKED", wrongPassword: "ERR_WRONG_PASSWORD", }; + this._db.ev.subscribe("user:loggedOut", () => { + this._password = ""; + }); } /** diff --git a/packages/core/collections/collection.js b/packages/core/collections/collection.js index bf74ee8d0..6e08cf091 100644 --- a/packages/core/collections/collection.js +++ b/packages/core/collections/collection.js @@ -8,7 +8,10 @@ class Collection { if (collection.init) await collection.init(); if (collection._collection.clear) - db.ev.subscribe("user:loggedOut", () => collection._collection.clear()); + db.ev.subscribe( + "user:loggedOut", + async () => await collection._collection.clear() + ); return collection; } diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js index 07492d78a..36092c1a0 100644 --- a/packages/core/database/cached-collection.js +++ b/packages/core/database/cached-collection.js @@ -14,8 +14,9 @@ export default class CachedCollection { if (data.length > 0) this.map = new Map(data); } - clear() { + async clear() { this.map.clear(); + await this.indexer.clear(); } /** diff --git a/packages/core/database/indexed-collection.js b/packages/core/database/indexed-collection.js index 5ab0dfc16..6f3b06f8e 100644 --- a/packages/core/database/indexed-collection.js +++ b/packages/core/database/indexed-collection.js @@ -5,6 +5,10 @@ export default class IndexedCollection { this.indexer = new Indexer(context, type); } + clear() { + return this.indexer.clear(); + } + async init() { await this.indexer.init(); } @@ -34,7 +38,7 @@ export default class IndexedCollection { id, deleted: true, dateCreated: Date.now(), - dateEdited: Date.now() + dateEdited: Date.now(), }); } diff --git a/packages/core/database/indexer.js b/packages/core/database/indexer.js index 62bb84605..5623b4657 100644 --- a/packages/core/database/indexer.js +++ b/packages/core/database/indexer.js @@ -28,4 +28,9 @@ export default class Indexer extends Storage { this.indices.splice(this.indices.indexOf(key), 1); await this.write(this.type, this.indices); } + + async clear() { + this.indices = []; + await super.clear(); + } }