diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-01-25 12:38:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-25 12:38:13 +0800 |
commit | d30612a2168b02c39a3eaa86f29e47d9edda07d8 (patch) | |
tree | 27eab17687c0c7a1b2583f51abf8064fa54de82a /app/scripts/metamask-controller.js | |
parent | 4f39e8192cd94ad45d68992d5d1129f1612b1aa6 (diff) | |
parent | 0f33acb80ca90e07e6f7b7c083f52a1f4344c48e (diff) | |
download | tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar.gz tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar.bz2 tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar.lz tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar.xz tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.tar.zst tangerine-wallet-browser-d30612a2168b02c39a3eaa86f29e47d9edda07d8.zip |
Merge pull request #999 from MetaMask/obs-store2
background - introduce ObservableStore (mark II)
Diffstat (limited to 'app/scripts/metamask-controller.js')
-rw-r--r-- | app/scripts/metamask-controller.js | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 5c663255a..8f157a45e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1,13 +1,15 @@ 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') 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') @@ -15,19 +17,30 @@ const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') const accountImporter = require('./account-import-strategies') + 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, @@ -245,29 +258,23 @@ 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, + const publicConfigStore = new ObservableStore() + + // sync publicConfigStore with transform + pipe( + this.store, + storeTransform(selectPublicState), + publicConfigStore + ) + + function selectPublicState(state) { + const result = { selectedAccount: undefined } + try { + result.selectedAccount = state.config.selectedAccount + } catch (_) { + // thats fine, im sure it will be there next time... } - } - // dump obj into store - function storeSetFromObj (store, obj) { - Object.keys(obj).forEach(function (key) { - store.set(key, obj[key]) - }) + return result } return publicConfigStore @@ -310,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 |