From a76324f6d397c3e746ba501cfd858c4869cb0af7 Mon Sep 17 00:00:00 2001 From: Ellie Day Date: Sat, 23 Dec 2017 08:23:34 -0600 Subject: Add ExtensionStore and add basic store instance syncing to main controller --- app/scripts/background.js | 12 ++++++++++++ app/scripts/lib/extension-store.js | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 app/scripts/lib/extension-store.js (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index da022c490..45da2f6d0 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -4,6 +4,7 @@ const pump = require('pump') const log = require('loglevel') const extension = require('extensionizer') const LocalStorageStore = require('obs-store/lib/localStorage') +const ExtensionStore = require('./lib/extension-store') const storeTransform = require('obs-store/lib/transform') const asStream = require('obs-store/lib/asStream') const ExtensionPlatform = require('./platforms/extension') @@ -28,6 +29,7 @@ let popupIsOpen = false // state persistence const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) +const extensionStore = new ExtensionStore() // initialization flow initialize().catch(log.error) @@ -45,8 +47,12 @@ async function initialize () { async function loadStateFromPersistence () { // migrations const migrator = new Migrator({ migrations }) + // fetch from extension store + const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) // read from disk let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) + // merge extension and versioned data + versionedData = { ...versionedData, ...extensionData } // migrate data versionedData = await migrator.migrateData(versionedData) // write to disk @@ -76,6 +82,7 @@ function setupController (initState) { pump( asStream(controller.store), storeTransform(versionifyData), + storeTransform(syncDataWithExtension), asStream(diskStore) ) @@ -85,6 +92,11 @@ function setupController (initState) { return versionedData } + function syncDataWithExtension(state) { + extensionStore.sync(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + return state + } + // // connect to other contexts // diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js new file mode 100644 index 000000000..a8b730a65 --- /dev/null +++ b/app/scripts/lib/extension-store.js @@ -0,0 +1,20 @@ +const extension = require('extensionizer') + +const KEYS_TO_SYNC = ['KeyringController', 'PreferencesController'] + +module.exports = class ExtensionStore { + async fetch() { + return new Promise((resolve) => { + extension.storage.sync.get(KEYS_TO_SYNC, data => resolve(data)) + }) + } + async sync(state) { + const dataToSync = KEYS_TO_SYNC.reduce((result, key) => { + result[key] = state.data[key] + return result + }, {}) + return new Promise((resolve) => { + extension.storage.sync.set(dataToSync, () => resolve()) + }) + } +} -- cgit v1.2.3 From 7184db7632ef79d4bde0e643fdc1a4ee910c77fb Mon Sep 17 00:00:00 2001 From: Ellie Day Date: Tue, 2 Jan 2018 21:31:17 -0800 Subject: handle situation where storage.sync is disabled in certain versions of firefox --- app/scripts/background.js | 5 ++--- app/scripts/lib/extension-store.js | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 45da2f6d0..732f47590 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -47,11 +47,10 @@ async function initialize () { async function loadStateFromPersistence () { // migrations const migrator = new Migrator({ migrations }) - // fetch from extension store - const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) // read from disk let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) - // merge extension and versioned data + // fetch from extension store and merge in data + const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) versionedData = { ...versionedData, ...extensionData } // migrate data versionedData = await migrator.migrateData(versionedData) diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js index a8b730a65..dd0f82f36 100644 --- a/app/scripts/lib/extension-store.js +++ b/app/scripts/lib/extension-store.js @@ -1,11 +1,24 @@ const extension = require('extensionizer') const KEYS_TO_SYNC = ['KeyringController', 'PreferencesController'] +const FIREFOX_SYNC_DISABLED_MESSAGE = 'Please set webextensions.storage.sync.enabled to true in about:config' + +const handleDisabledSyncAndResolve = (resolve, toResolve) => { + // Firefox 52 has sync available on extension.storage, but it is disabled by default + const lastError = extension.runtime.lastError + if (lastError && lastError.message.includes(FIREFOX_SYNC_DISABLED_MESSAGE)) { + resolve({}) + } else { + resolve(toResolve) + } +} module.exports = class ExtensionStore { async fetch() { return new Promise((resolve) => { - extension.storage.sync.get(KEYS_TO_SYNC, data => resolve(data)) + extension.storage.sync.get(KEYS_TO_SYNC, (data) => { + handleDisabledSyncAndResolve(resolve, data) + }) }) } async sync(state) { @@ -14,7 +27,9 @@ module.exports = class ExtensionStore { return result }, {}) return new Promise((resolve) => { - extension.storage.sync.set(dataToSync, () => resolve()) + extension.storage.sync.set(dataToSync, () => { + handleDisabledSyncAndResolve(resolve) + }) }) } } -- cgit v1.2.3 From 3c6a5b16ad37c83f548028d5b6fa3d0f75293ca5 Mon Sep 17 00:00:00 2001 From: Ellie Day Date: Tue, 2 Jan 2018 21:53:11 -0800 Subject: conditionally use extension store if supported or enabled --- app/scripts/background.js | 12 +++++++++--- app/scripts/lib/extension-store.js | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 732f47590..d9a2b0a6e 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -50,8 +50,12 @@ async function loadStateFromPersistence () { // read from disk let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) // fetch from extension store and merge in data - const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) - versionedData = { ...versionedData, ...extensionData } + + if (extensionStore.isSupported && extensionStore.isEnabled) { + const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + versionedData = { ...versionedData, ...extensionData } + } + // migrate data versionedData = await migrator.migrateData(versionedData) // write to disk @@ -92,7 +96,9 @@ function setupController (initState) { } function syncDataWithExtension(state) { - extensionStore.sync(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + if (extensionStore.isSupported && extensionStore.isEnabled) { + extensionStore.sync(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + } return state } diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js index dd0f82f36..67ee71f16 100644 --- a/app/scripts/lib/extension-store.js +++ b/app/scripts/lib/extension-store.js @@ -14,6 +14,10 @@ const handleDisabledSyncAndResolve = (resolve, toResolve) => { } module.exports = class ExtensionStore { + constructor() { + this.isSupported = !!(extension.storage.sync) + this.isEnabled = true // TODO: get value from user settings + } async fetch() { return new Promise((resolve) => { extension.storage.sync.get(KEYS_TO_SYNC, (data) => { -- cgit v1.2.3 From 03c64ba8a646cbc5a62f2b2a8c5881bb4a4bda60 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 23 Jan 2018 15:45:33 -0800 Subject: Add unlimitedStorage option to manifest --- app/manifest.json | 1 + 1 file changed, 1 insertion(+) (limited to 'app') diff --git a/app/manifest.json b/app/manifest.json index d795a225a..13ba074e7 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -60,6 +60,7 @@ "http://localhost:8545/", "https://*.infura.io/" ], + "unlimitedStorage": true, "web_accessible_resources": [ "scripts/inpage.js" ], -- cgit v1.2.3 From 456dfdb9fdc0b7b0637d50808beb85ae33602f5b Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 23 Jan 2018 16:26:50 -0800 Subject: Modify @heyellieday's work to use storage.local to replace main storage --- app/scripts/background.js | 15 ++++++++------- app/scripts/lib/local-store.js | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 app/scripts/lib/local-store.js (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index d9a2b0a6e..9790129aa 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -4,7 +4,7 @@ const pump = require('pump') const log = require('loglevel') const extension = require('extensionizer') const LocalStorageStore = require('obs-store/lib/localStorage') -const ExtensionStore = require('./lib/extension-store') +const LocalStore = require('./lib/local-store') const storeTransform = require('obs-store/lib/transform') const asStream = require('obs-store/lib/asStream') const ExtensionPlatform = require('./platforms/extension') @@ -29,7 +29,7 @@ let popupIsOpen = false // state persistence const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) -const extensionStore = new ExtensionStore() +const localStore = new LocalStore() // initialization flow initialize().catch(log.error) @@ -51,9 +51,10 @@ async function loadStateFromPersistence () { let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) // fetch from extension store and merge in data - if (extensionStore.isSupported && extensionStore.isEnabled) { - const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) - versionedData = { ...versionedData, ...extensionData } + if (localStore.isSupported) { + const localData = await localStore.get() + // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + versionedData = localData || versionedData } // migrate data @@ -96,8 +97,8 @@ function setupController (initState) { } function syncDataWithExtension(state) { - if (extensionStore.isSupported && extensionStore.isEnabled) { - extensionStore.sync(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + if (localStore.isSupported) { + localStore.set(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) } return state } diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js new file mode 100644 index 000000000..32faac96b --- /dev/null +++ b/app/scripts/lib/local-store.js @@ -0,0 +1,25 @@ +// We should not rely on local storage in an extension! +// We should use this instead! +// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local + +const extension = require('extensionizer') +const STORAGE_KEY = 'metamask-config' + +module.exports = class ExtensionStore { + constructor() { + this.isSupported = !!(extension.storage.local) + if (!this.isSupported) { + log.error('Storage local API not available.') + } + } + async get() { + return new Promise((resolve) => { + extension.storage.local.get(STORAGE_KEY, resolve) + }) + } + async set(state) { + return new Promise((resolve) => { + extension.storage.local.set(state, resolve) + }) + } +} -- cgit v1.2.3 From 0164030e56b1db8117a1a0bdff91987321b2cd1a Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 24 Jan 2018 09:41:32 -0330 Subject: Handle errors when getting and setting to localStore. --- app/scripts/background.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 280c28d70..88600bf1e 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -57,7 +57,13 @@ async function loadStateFromPersistence () { // fetch from extension store and merge in data if (localStore.isSupported) { - const localData = await localStore.get() + let localData + try { + localData = await localStore.get() + } catch (err) { + log.error('error fetching state from local store:', err) + } + // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) versionedData = Object.keys(localData).length > 0 ? localData : versionedData } @@ -113,7 +119,11 @@ function setupController (initState) { function syncDataWithExtension(state) { if (localStore.isSupported) { - localStore.set(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) + try { + localStore.set(state) + } catch (err) { + log.error('error setting state in local store:', err) + } } return state } -- cgit v1.2.3 From b7ae77f57a0e2bc68e9548364baa120805a1420c Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 24 Jan 2018 09:43:20 -0330 Subject: Check that extension.storage exists before attempting to call methods on it. --- app/scripts/lib/extension-store.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js index 67ee71f16..4a970321c 100644 --- a/app/scripts/lib/extension-store.js +++ b/app/scripts/lib/extension-store.js @@ -15,12 +15,12 @@ const handleDisabledSyncAndResolve = (resolve, toResolve) => { module.exports = class ExtensionStore { constructor() { - this.isSupported = !!(extension.storage.sync) + this.isSupported = !!(extension.storage && extension.storage.sync) this.isEnabled = true // TODO: get value from user settings } async fetch() { return new Promise((resolve) => { - extension.storage.sync.get(KEYS_TO_SYNC, (data) => { + extension.storage && extension.storage.sync.get(KEYS_TO_SYNC, (data) => { handleDisabledSyncAndResolve(resolve, data) }) }) @@ -31,7 +31,7 @@ module.exports = class ExtensionStore { return result }, {}) return new Promise((resolve) => { - extension.storage.sync.set(dataToSync, () => { + extension.storage && extension.storage.sync.set(dataToSync, () => { handleDisabledSyncAndResolve(resolve) }) }) -- cgit v1.2.3 From dd80bd48babc1bfebf91ac2350491b06971b1fc1 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 24 Jan 2018 11:36:15 -0800 Subject: Corrected unlimitedStorage permission --- app/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/manifest.json b/app/manifest.json index 13ba074e7..114586d0f 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -56,11 +56,11 @@ ], "permissions": [ "storage", + "unlimitedStorage", "clipboardWrite", "http://localhost:8545/", "https://*.infura.io/" ], - "unlimitedStorage": true, "web_accessible_resources": [ "scripts/inpage.js" ], -- cgit v1.2.3 From f09d72fa2aa88f0def76d228cb7d8eab29e3b092 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 24 Jan 2018 11:36:42 -0800 Subject: Remove extension-store since we aren't using it yet --- app/scripts/lib/extension-store.js | 39 -------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 app/scripts/lib/extension-store.js (limited to 'app') diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js deleted file mode 100644 index 4a970321c..000000000 --- a/app/scripts/lib/extension-store.js +++ /dev/null @@ -1,39 +0,0 @@ -const extension = require('extensionizer') - -const KEYS_TO_SYNC = ['KeyringController', 'PreferencesController'] -const FIREFOX_SYNC_DISABLED_MESSAGE = 'Please set webextensions.storage.sync.enabled to true in about:config' - -const handleDisabledSyncAndResolve = (resolve, toResolve) => { - // Firefox 52 has sync available on extension.storage, but it is disabled by default - const lastError = extension.runtime.lastError - if (lastError && lastError.message.includes(FIREFOX_SYNC_DISABLED_MESSAGE)) { - resolve({}) - } else { - resolve(toResolve) - } -} - -module.exports = class ExtensionStore { - constructor() { - this.isSupported = !!(extension.storage && extension.storage.sync) - this.isEnabled = true // TODO: get value from user settings - } - async fetch() { - return new Promise((resolve) => { - extension.storage && extension.storage.sync.get(KEYS_TO_SYNC, (data) => { - handleDisabledSyncAndResolve(resolve, data) - }) - }) - } - async sync(state) { - const dataToSync = KEYS_TO_SYNC.reduce((result, key) => { - result[key] = state.data[key] - return result - }, {}) - return new Promise((resolve) => { - extension.storage && extension.storage.sync.set(dataToSync, () => { - handleDisabledSyncAndResolve(resolve) - }) - }) - } -} -- cgit v1.2.3 From b281a5275983c4e2d924ba696c4885fd779d2c44 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 24 Jan 2018 16:49:12 -0330 Subject: Remove already handled TODO comment. --- app/scripts/background.js | 1 - 1 file changed, 1 deletion(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 88600bf1e..3e04a31b3 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -64,7 +64,6 @@ async function loadStateFromPersistence () { log.error('error fetching state from local store:', err) } - // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError) versionedData = Object.keys(localData).length > 0 ? localData : versionedData } -- cgit v1.2.3 From cd5eaa4393a122247295c7627a3fad3e678bea30 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 24 Jan 2018 13:05:13 -0800 Subject: Remove redundant async modifiers --- app/scripts/lib/local-store.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js index 32faac96b..9114364b6 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -12,12 +12,12 @@ module.exports = class ExtensionStore { log.error('Storage local API not available.') } } - async get() { + get() { return new Promise((resolve) => { extension.storage.local.get(STORAGE_KEY, resolve) }) } - async set(state) { + set(state) { return new Promise((resolve) => { extension.storage.local.set(state, resolve) }) -- cgit v1.2.3 From 76521cf7399c1e694a7202dcb9725ed5e1e2a0d7 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 24 Jan 2018 15:03:16 -0800 Subject: Fix retrieval of object --- app/scripts/lib/local-store.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app') diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js index 9114364b6..9e8d8db37 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -3,7 +3,6 @@ // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local const extension = require('extensionizer') -const STORAGE_KEY = 'metamask-config' module.exports = class ExtensionStore { constructor() { @@ -14,7 +13,7 @@ module.exports = class ExtensionStore { } get() { return new Promise((resolve) => { - extension.storage.local.get(STORAGE_KEY, resolve) + extension.storage.local.get(null, resolve) }) } set(state) { -- cgit v1.2.3 From 2f13790653cb20d9d967700133df6cf31ff02d14 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 24 Jan 2018 15:28:15 -0800 Subject: Remove local storage writes, add log --- app/scripts/background.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 3e04a31b3..a77763c41 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -35,6 +35,7 @@ let popupIsOpen = false // state persistence const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) const localStore = new LocalStore() +let versionedData // initialization flow initialize().catch(log.error) @@ -53,7 +54,7 @@ async function loadStateFromPersistence () { // migrations const migrator = new Migrator({ migrations }) // read from disk - let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) + versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) // fetch from extension store and merge in data if (localStore.isSupported) { @@ -64,13 +65,24 @@ async function loadStateFromPersistence () { log.error('error fetching state from local store:', err) } - versionedData = Object.keys(localData).length > 0 ? localData : versionedData + console.log('Comparing localdata and versionedData') + console.dir({ localData }) + + if (Object.keys(localData).length > 0) { + console.log('using the local store data') + versionedData = localData + } } // migrate data versionedData = await migrator.migrateData(versionedData) + // write to disk - diskStore.putState(versionedData) + localStore.set(versionedData) + .catch((reason) => { + log.error('Problem saving migrated data', versionedData) + }) + // return just the data return versionedData.data } @@ -107,11 +119,9 @@ function setupController (initState) { asStream(controller.store), storeTransform(versionifyData), storeTransform(syncDataWithExtension), - asStream(diskStore) ) function versionifyData (state) { - const versionedData = diskStore.getState() versionedData.data = state return versionedData } @@ -119,6 +129,7 @@ function setupController (initState) { function syncDataWithExtension(state) { if (localStore.isSupported) { try { + console.log('persisting state', state) localStore.set(state) } catch (err) { log.error('error setting state in local store:', err) -- cgit v1.2.3 From ceebc6caa4f3eab1cf6c9ec8f47dc8bd450ca4a9 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 25 Jan 2018 13:01:03 -0800 Subject: Debounce storage to avoid crashing pump --- app/scripts/background.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index a77763c41..2a8efd844 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,6 +1,7 @@ const urlUtil = require('url') const endOfStream = require('end-of-stream') const pump = require('pump') +const debounce = require('debounce-stream') const log = require('loglevel') const extension = require('extensionizer') const LocalStorageStore = require('obs-store/lib/localStorage') @@ -65,11 +66,7 @@ async function loadStateFromPersistence () { log.error('error fetching state from local store:', err) } - console.log('Comparing localdata and versionedData') - console.dir({ localData }) - if (Object.keys(localData).length > 0) { - console.log('using the local store data') versionedData = localData } } @@ -117,8 +114,12 @@ function setupController (initState) { // setup state persistence pump( asStream(controller.store), + debounce(200), storeTransform(versionifyData), storeTransform(syncDataWithExtension), + (error) => { + log.error('pump hit error', error) + } ) function versionifyData (state) { @@ -126,15 +127,14 @@ function setupController (initState) { return versionedData } - function syncDataWithExtension(state) { + async function syncDataWithExtension(state) { if (localStore.isSupported) { try { - console.log('persisting state', state) - localStore.set(state) + await localStore.set(state) } catch (err) { log.error('error setting state in local store:', err) } - } + } else { log.error('local store not supported') } return state } -- cgit v1.2.3 From 8ba64c657ff801425c06db166e2c9a06289047de Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 25 Jan 2018 15:38:43 -0800 Subject: Increase storage debounce value --- app/scripts/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 2a8efd844..07a260a4f 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -114,7 +114,7 @@ function setupController (initState) { // setup state persistence pump( asStream(controller.store), - debounce(200), + debounce(1000), storeTransform(versionifyData), storeTransform(syncDataWithExtension), (error) => { -- cgit v1.2.3 From ab55c184c7e40e6e46bc4da5d8438a18999a266e Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 7 Mar 2018 13:50:22 -0800 Subject: Increase storage debounce to 2s --- app/scripts/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 464330708..cb6810173 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -126,7 +126,7 @@ function setupController (initState) { // setup state persistence pump( asStream(controller.store), - debounce(1000), + debounce(2000), storeTransform(versionifyData), storeTransform(syncDataWithExtension), (error) => { -- cgit v1.2.3 From d58e47edec80180c7dd99effc229e116c63b4f57 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 7 Mar 2018 14:23:31 -0800 Subject: Some fixes for new storage strategy - Don't persist undefined data - Write to new storage strategy without waiting for completion. - Continue writing to localStorage as fallback. --- app/scripts/background.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index cb6810173..b5f7002dc 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -93,6 +93,9 @@ async function loadStateFromPersistence () { .catch((reason) => { log.error('Problem saving migrated data', versionedData) }) + if (versionedData) { + diskStore.putState(versionedData) + } // return just the data return versionedData.data @@ -129,6 +132,7 @@ function setupController (initState) { debounce(2000), storeTransform(versionifyData), storeTransform(syncDataWithExtension), + asStream(diskStore), (error) => { log.error('pump hit error', error) } @@ -139,14 +143,14 @@ function setupController (initState) { return versionedData } - async function syncDataWithExtension(state) { + function syncDataWithExtension(state) { if (localStore.isSupported) { - try { - await localStore.set(state) - } catch (err) { + localStore.set(state) + .catch((err) => { log.error('error setting state in local store:', err) - } + }) } else { log.error('local store not supported') } + return state } -- cgit v1.2.3 From b97ac5106fd8479a81dc5c32ede99090427ee1b8 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 7 Mar 2018 14:38:26 -0800 Subject: Add clarifying comment --- app/scripts/background.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index b5f7002dc..55eeed83f 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -80,6 +80,7 @@ async function loadStateFromPersistence () { 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) { versionedData = localData } -- cgit v1.2.3 From 64fcd271028ff57c34dd930b76ce23081eff814a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 7 Mar 2018 14:42:59 -0800 Subject: Tighten up data loading code --- app/scripts/background.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/scripts/background.js b/app/scripts/background.js index 55eeed83f..e8a7594ac 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -68,10 +68,10 @@ async function initialize () { async function loadStateFromPersistence () { // migrations const migrator = new Migrator({ migrations }) - // read from disk - versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState) - // fetch from extension store and merge in data + // read from disk + // first from preferred, async API: + let localStoreData if (localStore.isSupported) { let localData try { @@ -82,10 +82,14 @@ async function loadStateFromPersistence () { // If localStore is supported but has not been written to yet, ignore: if (Object.keys(localData).length > 0) { - versionedData = localData + localStoreData = localData } } + versionedData = localStoreData || + diskStore.getState() || + migrator.generateInitialState(firstTimeState) + // migrate data versionedData = await migrator.migrateData(versionedData) -- cgit v1.2.3 From 98efca0a9798db205ef8068c038a225a79c575cd Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 8 Mar 2018 14:10:28 -0800 Subject: background - storage - cleanup storage wiring --- app/scripts/background.js | 34 ++++++++-------------------------- app/scripts/lib/local-store.js | 30 ++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 34 deletions(-) (limited to 'app') 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 +} -- cgit v1.2.3 From a88e436b7d45d9a7f9f4d4a4be58aff5e58e9074 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 8 Mar 2018 14:55:35 -0800 Subject: lint fix --- app/scripts/lib/local-store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js index 73482a636..781aea17e 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -34,5 +34,5 @@ module.exports = class ExtensionStore { } function isEmpty(obj) { - return 0 === Object.keys(obj).length + return Object.keys(obj).length === 0 } -- cgit v1.2.3