diff options
author | Ellie Day <heyellieday@gmail.com> | 2017-12-23 22:23:34 +0800 |
---|---|---|
committer | Ellie Day <heyellieday@gmail.com> | 2017-12-23 22:23:34 +0800 |
commit | a76324f6d397c3e746ba501cfd858c4869cb0af7 (patch) | |
tree | f29403ceaa71a7c374046d223b79f032e6045604 /app | |
parent | d3f5ad874e4bf90cda4e440f9b0635c6ac416382 (diff) | |
download | tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar.gz tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar.bz2 tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar.lz tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar.xz tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.tar.zst tangerine-wallet-browser-a76324f6d397c3e746ba501cfd858c4869cb0af7.zip |
Add ExtensionStore and add basic store instance syncing to main controller
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/background.js | 12 | ||||
-rw-r--r-- | app/scripts/lib/extension-store.js | 20 |
2 files changed, 32 insertions, 0 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index da022c490..45da2f6d0 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -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 // diff --git a/app/scripts/lib/extension-store.js b/app/scripts/lib/extension-store.js new file mode 100644 index 000000000..a8b730a65 --- /dev/null +++ b/app/scripts/lib/extension-store.js @@ -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()) + }) + } +} |