background - storage - cleanup storage wiring
This commit is contained in:
parent
64fcd27102
commit
98efca0a97
|
@ -71,36 +71,19 @@ async function loadStateFromPersistence () {
|
|||
|
||||
// read from disk
|
||||
// first from preferred, async API:
|
||||
let localStoreData
|
||||
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 ||
|
||||
versionedData = (await localStore.get()) ||
|
||||
diskStore.getState() ||
|
||||
migrator.generateInitialState(firstTimeState)
|
||||
|
||||
// migrate data
|
||||
versionedData = await migrator.migrateData(versionedData)
|
||||
if (!versionedData) {
|
||||
throw new Error('MetaMask - migrator returned undefined')
|
||||
}
|
||||
|
||||
// write to disk
|
||||
localStore.set(versionedData)
|
||||
.catch((reason) => {
|
||||
log.error('Problem saving migrated data', versionedData)
|
||||
})
|
||||
if (versionedData) {
|
||||
diskStore.putState(versionedData)
|
||||
}
|
||||
if (localStore.isSupported) localStore.set(versionedData)
|
||||
diskStore.putState(versionedData)
|
||||
|
||||
// return just the data
|
||||
return versionedData.data
|
||||
|
@ -134,7 +117,7 @@ function setupController (initState) {
|
|||
// setup state persistence
|
||||
pump(
|
||||
asStream(controller.store),
|
||||
debounce(2000),
|
||||
debounce(1000),
|
||||
storeTransform(versionifyData),
|
||||
storeTransform(syncDataWithExtension),
|
||||
asStream(diskStore),
|
||||
|
@ -154,8 +137,7 @@ function setupController (initState) {
|
|||
.catch((err) => {
|
||||
log.error('error setting state in local store:', err)
|
||||
})
|
||||
} else { log.error('local store not supported') }
|
||||
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue