From 8012ede12698477692b80769781096b559159a32 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 11 Jan 2017 19:04:19 -0800 Subject: background - introduce ObservableStore --- app/scripts/metamask-controller.js | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1fc97e81d..8e0eaf54c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -6,26 +6,38 @@ const KeyringController = require('./keyring-controller') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') -const HostStore = require('./lib/remote-store.js').HostStore const Web3 = require('web3') const ConfigManager = require('./lib/config-manager') const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') +const ObservableStore = require('./lib/observable/') +const HostStore = require('./lib/observable/host') +const transformStore = require('./lib/observable/util/transform') const version = require('../manifest.json').version module.exports = class MetamaskController extends EventEmitter { constructor (opts) { super() - this.state = { network: 'loading' } this.opts = opts - this.configManager = new ConfigManager(opts) + this.state = { network: 'loading' } + + // observable state store + this.store = new ObservableStore(opts.initState) + // config manager + this.configManager = new ConfigManager({ + store: this.store, + }) + // key mgmt this.keyringController = new KeyringController({ configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) + this.keyringController.on('newAccount', (account) => { + autoFaucet(account) + }) // notices this.noticeController = new NoticeController({ configManager: this.configManager, @@ -228,29 +240,20 @@ module.exports = class MetamaskController extends EventEmitter { initPublicConfigStore () { // get init state - var initPublicState = configToPublic(this.configManager.getConfig()) - var publicConfigStore = new HostStore(initPublicState) - - // subscribe to changes - this.configManager.subscribe(function (state) { - storeSetFromObj(publicConfigStore, configToPublic(state)) - }) - - this.keyringController.on('newAccount', (account) => { - autoFaucet(account) - }) - - // config substate - function configToPublic (state) { - return { - selectedAccount: state.selectedAccount, + var initPublicState = this.store.get() + var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) + + // sync publicConfigStore with transform + transformStore(this.store, publicConfigStore, selectPublicState) + + function selectPublicState(state) { + let result = { selectedAccount: undefined } + try { + result.selectedAccount = state.data.config.selectedAccount + } catch (err) { + console.warn('Error in "selectPublicState": ' + err.message) } - } - // dump obj into store - function storeSetFromObj (store, obj) { - Object.keys(obj).forEach(function (key) { - store.set(key, obj[key]) - }) + return result } return publicConfigStore -- cgit v1.2.3 From 3bc996878b467e1fa5fd63656bd465377daa137d Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 11 Jan 2017 22:47:56 -0800 Subject: background - move pojo migrator to outside of metamask controller --- app/scripts/metamask-controller.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8e0eaf54c..e15844a56 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -14,7 +14,7 @@ const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') const ObservableStore = require('./lib/observable/') const HostStore = require('./lib/observable/host') -const transformStore = require('./lib/observable/util/transform') +const synchronizeStore = require('./lib/observable/util/sync') const version = require('../manifest.json').version module.exports = class MetamaskController extends EventEmitter { @@ -244,12 +244,12 @@ module.exports = class MetamaskController extends EventEmitter { var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) // sync publicConfigStore with transform - transformStore(this.store, publicConfigStore, selectPublicState) + synchronizeStore(this.store, publicConfigStore, selectPublicState) function selectPublicState(state) { let result = { selectedAccount: undefined } try { - result.selectedAccount = state.data.config.selectedAccount + result.selectedAccount = state.config.selectedAccount } catch (err) { console.warn('Error in "selectPublicState": ' + err.message) } -- cgit v1.2.3 From 76ce348a04b83693eda0e8a40f9888c1f5fe7ef5 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 24 Jan 2017 19:47:00 -0800 Subject: obs-store - use published module --- app/scripts/metamask-controller.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index d3077817d..8f157a45e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1,6 +1,9 @@ const EventEmitter = require('events') const extend = require('xtend') const promiseToCallback = require('promise-to-callback') +const pipe = require('pump') +const ObservableStore = require('obs-store') +const storeTransform = require('obs-store/lib/transform') const EthStore = require('./lib/eth-store') const MetaMaskProvider = require('web3-provider-engine/zero.js') const KeyringController = require('./keyring-controller') @@ -13,9 +16,6 @@ const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') -const ObservableStore = require('./lib/observable/') -const HostStore = require('./lib/observable/host') -const synchronizeStore = require('./lib/observable/util/sync') const accountImporter = require('./account-import-strategies') const version = require('../manifest.json').version @@ -258,18 +258,21 @@ module.exports = class MetamaskController extends EventEmitter { initPublicConfigStore () { // get init state - var initPublicState = this.store.get() - var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) + const publicConfigStore = new ObservableStore() // sync publicConfigStore with transform - synchronizeStore(this.store, publicConfigStore, selectPublicState) + pipe( + this.store, + storeTransform(selectPublicState), + publicConfigStore + ) function selectPublicState(state) { - let result = { selectedAccount: undefined } + const result = { selectedAccount: undefined } try { result.selectedAccount = state.config.selectedAccount - } catch (err) { - console.warn('Error in "selectPublicState": ' + err.message) + } catch (_) { + // thats fine, im sure it will be there next time... } return result } @@ -314,9 +317,11 @@ module.exports = class MetamaskController extends EventEmitter { this.opts.showUnconfirmedMessage(msgParams, msgId) } - setupPublicConfig (stream) { - var storeStream = this.publicConfigStore.createStream() - stream.pipe(storeStream).pipe(stream) + setupPublicConfig (outStream) { + pipe( + this.publicConfigStore, + outStream + ) } // Log blocks -- cgit v1.2.3