mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add ExtensionStore and add basic store instance syncing to main controller
This commit is contained in:
parent
d3f5ad874e
commit
a76324f6d3
@ -4,6 +4,7 @@ const pump = require('pump')
|
||||
const log = require('loglevel')
|
||||
const extension = require('extensionizer')
|
||||
const LocalStorageStore = require('obs-store/lib/localStorage')
|
||||
const ExtensionStore = require('./lib/extension-store')
|
||||
const storeTransform = require('obs-store/lib/transform')
|
||||
const asStream = require('obs-store/lib/asStream')
|
||||
const ExtensionPlatform = require('./platforms/extension')
|
||||
@ -28,6 +29,7 @@ let popupIsOpen = false
|
||||
|
||||
// state persistence
|
||||
const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY })
|
||||
const extensionStore = new ExtensionStore()
|
||||
|
||||
// initialization flow
|
||||
initialize().catch(log.error)
|
||||
@ -45,8 +47,12 @@ async function initialize () {
|
||||
async function loadStateFromPersistence () {
|
||||
// migrations
|
||||
const migrator = new Migrator({ migrations })
|
||||
// fetch from extension store
|
||||
const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError)
|
||||
// read from disk
|
||||
let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState)
|
||||
// merge extension and versioned data
|
||||
versionedData = { ...versionedData, ...extensionData }
|
||||
// migrate data
|
||||
versionedData = await migrator.migrateData(versionedData)
|
||||
// write to disk
|
||||
@ -76,6 +82,7 @@ function setupController (initState) {
|
||||
pump(
|
||||
asStream(controller.store),
|
||||
storeTransform(versionifyData),
|
||||
storeTransform(syncDataWithExtension),
|
||||
asStream(diskStore)
|
||||
)
|
||||
|
||||
@ -85,6 +92,11 @@ function setupController (initState) {
|
||||
return versionedData
|
||||
}
|
||||
|
||||
function syncDataWithExtension(state) {
|
||||
extensionStore.sync(state) // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError)
|
||||
return state
|
||||
}
|
||||
|
||||
//
|
||||
// connect to other contexts
|
||||
//
|
||||
|
20
app/scripts/lib/extension-store.js
Normal file
20
app/scripts/lib/extension-store.js
Normal file
@ -0,0 +1,20 @@
|
||||
const extension = require('extensionizer')
|
||||
|
||||
const KEYS_TO_SYNC = ['KeyringController', 'PreferencesController']
|
||||
|
||||
module.exports = class ExtensionStore {
|
||||
async fetch() {
|
||||
return new Promise((resolve) => {
|
||||
extension.storage.sync.get(KEYS_TO_SYNC, data => 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, () => resolve())
|
||||
})
|
||||
}
|
||||
}
|
29
test/unit/extension-store-test.js
Normal file
29
test/unit/extension-store-test.js
Normal file
@ -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
Block a user