aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/local-store.js
diff options
context:
space:
mode:
authorLe Quoc Viet <vietlq85@gmail.com>2018-03-15 16:11:42 +0800
committerGitHub <noreply@github.com>2018-03-15 16:11:42 +0800
commit04079455e36e48433cf8055c8f1f79e1e7e18298 (patch)
treed7de2a6603b67b56abacf09bee4d2bbbfe886b8f /app/scripts/lib/local-store.js
parent5bdee96e73f65a0b369277e9c56b0afe5159e65b (diff)
parente2efc91aee64072c408ab509219dcbfb389c7609 (diff)
downloadtangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar.gz
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar.bz2
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar.lz
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar.xz
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.tar.zst
tangerine-wallet-browser-04079455e36e48433cf8055c8f1f79e1e7e18298.zip
Merge pull request #1 from MetaMask/master
Merge from the source
Diffstat (limited to 'app/scripts/lib/local-store.js')
-rw-r--r--app/scripts/lib/local-store.js62
1 files changed, 62 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..5b47985f6
--- /dev/null
+++ b/app/scripts/lib/local-store.js
@@ -0,0 +1,62 @@
+// 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')
+
+module.exports = class ExtensionStore {
+ constructor() {
+ this.isSupported = !!(extension.storage.local)
+ if (!this.isSupported) {
+ log.error('Storage local API not available.')
+ }
+ }
+
+ 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)
+ }
+
+ _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)
+ }
+ })
+ })
+ }
+
+ _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) {
+ return Object.keys(obj).length === 0
+}