commit
7c4a9c32fa
@ -0,0 +1,39 @@ |
||||
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.sync) |
||||
this.isEnabled = true // TODO: get value from user settings
|
||||
} |
||||
async fetch() { |
||||
return new Promise((resolve) => { |
||||
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.sync.set(dataToSync, () => { |
||||
handleDisabledSyncAndResolve(resolve) |
||||
}) |
||||
}) |
||||
} |
||||
} |
@ -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) |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
const assert = require('assert') |
||||
|
||||
const ExtensionStore = require('../../app/scripts/lib/extension-store') |
||||
|
||||
describe('Extension Store', function () { |
||||
let extensionStore |
||||
|
||||
beforeEach(function () { |
||||
extensionStore = new ExtensionStore() |
||||
}) |
||||
|
||||
describe('#fetch', function () { |
||||
it('should return a promise', function () { |
||||
|
||||
}) |
||||
it('after promise resolution, should have loaded the proper data from the extension', function () { |
||||
|
||||
}) |
||||
}) |
||||
|
||||
describe('#sync', function () { |
||||
it('should return a promise', function () { |
||||
|
||||
}) |
||||
it('after promise resolution, should have synced the proper data from the extension', function () { |
||||
|
||||
}) |
||||
}) |
||||
}) |
Loading…
Reference in new issue