aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/metamask-controller.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-01-12 11:04:19 +0800
committerkumavis <aaron@kumavis.me>2017-01-12 11:31:21 +0800
commit8012ede12698477692b80769781096b559159a32 (patch)
tree17f75325da8f5d0abf2c1f37fa0573320730b6a7 /app/scripts/metamask-controller.js
parentcc5e9aca4fa2a1e78c49be680405d93ac918bccf (diff)
downloadtangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.gz
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.bz2
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.lz
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.xz
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.zst
tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.zip
background - introduce ObservableStore
Diffstat (limited to 'app/scripts/metamask-controller.js')
-rw-r--r--app/scripts/metamask-controller.js53
1 files changed, 28 insertions, 25 deletions
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