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 From b60992412546b6151d9c992ae2d34bb9e1ebe496 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 14 Mar 2018 08:21:50 -0700 Subject: fix destructuring of variables --- 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 781aea17e..1cf00dd30 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -3,7 +3,7 @@ // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local const extension = require('extensionizer') -const { promisify } = require('util').promisify +const { promisify } = require('util') module.exports = class ExtensionStore { constructor() { -- cgit v1.2.3 From 481983aae873d1fb7cceef2cb42ec4fb0580368a Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 14 Mar 2018 10:31:25 -0700 Subject: local-store - fix promisification of methods --- app/scripts/lib/local-store.js | 5 ++--- 1 file changed, 2 insertions(+), 3 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 1cf00dd30..2d6155dbf 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 { promisify } = require('util') module.exports = class ExtensionStore { constructor() { @@ -12,8 +11,8 @@ module.exports = class ExtensionStore { 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) + this._get = function() { return new Promise((resolve) => local.get(resolve)) } + this._set = function(state) { return new Promise((resolve) => local.set(state, resolve)) } } async get() { -- cgit v1.2.3 From c4e90c9359de3b4c54622bde3f396bc753c9cffb Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 14 Mar 2018 10:49:54 -0700 Subject: local-store - check for error from extension.runtime --- app/scripts/lib/local-store.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 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 2d6155dbf..175126d86 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -10,9 +10,6 @@ module.exports = class ExtensionStore { if (!this.isSupported) { log.error('Storage local API not available.') } - const local = extension.storage.local - this._get = function() { return new Promise((resolve) => local.get(resolve)) } - this._set = function(state) { return new Promise((resolve) => local.set(state, resolve)) } } async get() { @@ -30,6 +27,34 @@ module.exports = class ExtensionStore { async set(state) { return this._set(state) } + + function _get() { + const local = extension.storage.local + return new Promise((resolve, reject) => { + local.get(null, (result) => { + const err = extension.runtime.lastError + if (err) { + reject(err) + } else { + resolve(result) + } + }) + }) + } + + function _set(obj) { + const local = extension.storage.local + return new Promise((resolve, reject) => { + local.set(obj, () => { + const err = extension.runtime.lastError + if (err) { + reject(err) + } else { + resolve() + } + }) + }) + } } function isEmpty(obj) { -- cgit v1.2.3 From 6aef2ed0b26861b3402e13d4e2a7e401ac3e99c8 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 14 Mar 2018 10:59:47 -0700 Subject: local-store - fix class syntax --- 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 175126d86..5b47985f6 100644 --- a/app/scripts/lib/local-store.js +++ b/app/scripts/lib/local-store.js @@ -28,7 +28,7 @@ module.exports = class ExtensionStore { return this._set(state) } - function _get() { + _get() { const local = extension.storage.local return new Promise((resolve, reject) => { local.get(null, (result) => { @@ -42,7 +42,7 @@ module.exports = class ExtensionStore { }) } - function _set(obj) { + _set(obj) { const local = extension.storage.local return new Promise((resolve, reject) => { local.set(obj, () => { -- cgit v1.2.3