aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2018-03-09 06:10:28 +0800
committerkumavis <aaron@kumavis.me>2018-03-09 06:10:28 +0800
commit98efca0a9798db205ef8068c038a225a79c575cd (patch)
tree4ec379cefe4c5e0a1838b2a89fe107cc4ee129c4 /app
parent64fcd271028ff57c34dd930b76ce23081eff814a (diff)
downloadtangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar.gz
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar.bz2
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar.lz
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar.xz
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.tar.zst
tangerine-wallet-browser-98efca0a9798db205ef8068c038a225a79c575cd.zip
background - storage - cleanup storage wiring
Diffstat (limited to 'app')
-rw-r--r--app/scripts/background.js34
-rw-r--r--app/scripts/lib/local-store.js30
2 files changed, 30 insertions, 34 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js
index e8a7594ac..ef5513ec7 100644
--- a/app/scripts/background.js
+++ b/app/scripts/background.js
@@ -71,36 +71,19 @@ async function loadStateFromPersistence () {
// read from disk
// first from preferred, async API:
- let localStoreData
- if (localStore.isSupported) {
- let localData
- try {
- localData = await localStore.get()
- } catch (err) {
- log.error('error fetching state from local store:', err)
- }
-
- // If localStore is supported but has not been written to yet, ignore:
- if (Object.keys(localData).length > 0) {
- localStoreData = localData
- }
- }
-
- versionedData = localStoreData ||
+ versionedData = (await localStore.get()) ||
diskStore.getState() ||
migrator.generateInitialState(firstTimeState)
// migrate data
versionedData = await migrator.migrateData(versionedData)
+ if (!versionedData) {
+ throw new Error('MetaMask - migrator returned undefined')
+ }
// write to disk
- localStore.set(versionedData)
- .catch((reason) => {
- log.error('Problem saving migrated data', versionedData)
- })
- if (versionedData) {
- diskStore.putState(versionedData)
- }
+ if (localStore.isSupported) localStore.set(versionedData)
+ diskStore.putState(versionedData)
// return just the data
return versionedData.data
@@ -134,7 +117,7 @@ function setupController (initState) {
// setup state persistence
pump(
asStream(controller.store),
- debounce(2000),
+ debounce(1000),
storeTransform(versionifyData),
storeTransform(syncDataWithExtension),
asStream(diskStore),
@@ -154,8 +137,7 @@ function setupController (initState) {
.catch((err) => {
log.error('error setting state in local store:', err)
})
- } else { log.error('local store not supported') }
-
+ }
return state
}
diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js
index 9e8d8db37..73482a636 100644
--- a/app/scripts/lib/local-store.js
+++ b/app/scripts/lib/local-store.js
@@ -3,6 +3,7 @@
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local
const extension = require('extensionizer')
+const { promisify } = require('util').promisify
module.exports = class ExtensionStore {
constructor() {
@@ -10,15 +11,28 @@ module.exports = class ExtensionStore {
if (!this.isSupported) {
log.error('Storage local API not available.')
}
+ const local = extension.storage.local
+ this._get = promisify(local.get).bind(local)
+ this._set = promisify(local.set).bind(local)
}
- get() {
- return new Promise((resolve) => {
- extension.storage.local.get(null, resolve)
- })
+
+ async get() {
+ if (!this.isSupported) return undefined
+ const result = await this._get()
+ // extension.storage.local always returns an obj
+ // if the object is empty, treat it as undefined
+ if (isEmpty(result)) {
+ return undefined
+ } else {
+ return result
+ }
}
- set(state) {
- return new Promise((resolve) => {
- extension.storage.local.set(state, resolve)
- })
+
+ async set(state) {
+ return this._set(state)
}
}
+
+function isEmpty(obj) {
+ return 0 === Object.keys(obj).length
+}