diff options
author | kumavis <kumavis@users.noreply.github.com> | 2018-03-14 06:47:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-14 06:47:52 +0800 |
commit | 5fbfb0b67c9eb50b6cc503e78154ab237d2d9731 (patch) | |
tree | 3374055e365337973807afd70936afcad070a095 /app/scripts/lib/local-store.js | |
parent | b7a24840f2aa358a174d134d7ed30dc3ff2fc4be (diff) | |
parent | dc5fbf5eb28b3221357197d7fe33ecb2f8a1e6fb (diff) | |
download | tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar.gz tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar.bz2 tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar.lz tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar.xz tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.tar.zst tangerine-wallet-browser-5fbfb0b67c9eb50b6cc503e78154ab237d2d9731.zip |
Merge pull request #3083 from MetaMask/i3076-UseStorageLocalInstead
Use storage.local instead of localStorage
Diffstat (limited to 'app/scripts/lib/local-store.js')
-rw-r--r-- | app/scripts/lib/local-store.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/scripts/lib/local-store.js b/app/scripts/lib/local-store.js new file mode 100644 index 000000000..781aea17e --- /dev/null +++ b/app/scripts/lib/local-store.js @@ -0,0 +1,38 @@ +// 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 { promisify } = require('util').promisify + +module.exports = class ExtensionStore { + constructor() { + this.isSupported = !!(extension.storage.local) + 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) + } + + 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 + } + } + + async set(state) { + return this._set(state) + } +} + +function isEmpty(obj) { + return Object.keys(obj).length === 0 +} |