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/lib/local-store.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/scripts/lib/local-store.js (limited to 'app/scripts/lib/local-store.js') 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 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/scripts/lib/local-store.js') 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/scripts/lib/local-store.js') 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 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/lib/local-store.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'app/scripts/lib/local-store.js') 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/scripts/lib/local-store.js') 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