background - storage - cleanup storage wiring
This commit is contained in:
parent
64fcd27102
commit
98efca0a97
|
@ -71,36 +71,19 @@ async function loadStateFromPersistence () {
|
||||||
|
|
||||||
// read from disk
|
// read from disk
|
||||||
// first from preferred, async API:
|
// first from preferred, async API:
|
||||||
let localStoreData
|
versionedData = (await localStore.get()) ||
|
||||||
if (localStore.isSupported) {
|
|
||||||
let localData
|
|
||||||
try {
|
|
||||||
localData = await localStore.get()
|
|
||||||
} catch (err) {
|
|
||||||
log.error('error fetching state from local store:', err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If localStore is supported but has not been written to yet, ignore:
|
|
||||||
if (Object.keys(localData).length > 0) {
|
|
||||||
localStoreData = localData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
versionedData = localStoreData ||
|
|
||||||
diskStore.getState() ||
|
diskStore.getState() ||
|
||||||
migrator.generateInitialState(firstTimeState)
|
migrator.generateInitialState(firstTimeState)
|
||||||
|
|
||||||
// migrate data
|
// migrate data
|
||||||
versionedData = await migrator.migrateData(versionedData)
|
versionedData = await migrator.migrateData(versionedData)
|
||||||
|
if (!versionedData) {
|
||||||
|
throw new Error('MetaMask - migrator returned undefined')
|
||||||
|
}
|
||||||
|
|
||||||
// write to disk
|
// write to disk
|
||||||
localStore.set(versionedData)
|
if (localStore.isSupported) localStore.set(versionedData)
|
||||||
.catch((reason) => {
|
diskStore.putState(versionedData)
|
||||||
log.error('Problem saving migrated data', versionedData)
|
|
||||||
})
|
|
||||||
if (versionedData) {
|
|
||||||
diskStore.putState(versionedData)
|
|
||||||
}
|
|
||||||
|
|
||||||
// return just the data
|
// return just the data
|
||||||
return versionedData.data
|
return versionedData.data
|
||||||
|
@ -134,7 +117,7 @@ function setupController (initState) {
|
||||||
// setup state persistence
|
// setup state persistence
|
||||||
pump(
|
pump(
|
||||||
asStream(controller.store),
|
asStream(controller.store),
|
||||||
debounce(2000),
|
debounce(1000),
|
||||||
storeTransform(versionifyData),
|
storeTransform(versionifyData),
|
||||||
storeTransform(syncDataWithExtension),
|
storeTransform(syncDataWithExtension),
|
||||||
asStream(diskStore),
|
asStream(diskStore),
|
||||||
|
@ -154,8 +137,7 @@ function setupController (initState) {
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.error('error setting state in local store:', err)
|
log.error('error setting state in local store:', err)
|
||||||
})
|
})
|
||||||
} else { log.error('local store not supported') }
|
}
|
||||||
|
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local
|
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local
|
||||||
|
|
||||||
const extension = require('extensionizer')
|
const extension = require('extensionizer')
|
||||||
|
const { promisify } = require('util').promisify
|
||||||
|
|
||||||
module.exports = class ExtensionStore {
|
module.exports = class ExtensionStore {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -10,15 +11,28 @@ module.exports = class ExtensionStore {
|
||||||
if (!this.isSupported) {
|
if (!this.isSupported) {
|
||||||
log.error('Storage local API not available.')
|
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) => {
|
async get() {
|
||||||
extension.storage.local.get(null, resolve)
|
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) => {
|
async set(state) {
|
||||||
extension.storage.local.set(state, resolve)
|
return this._set(state)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isEmpty(obj) {
|
||||||
|
return 0 === Object.keys(obj).length
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue