1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

MetaMask Controller - Convert to EventEmitter

This commit is contained in:
Frankie 2017-01-05 11:06:18 -08:00
parent c22da8450c
commit e62f70660d
2 changed files with 8 additions and 12 deletions

View File

@ -22,7 +22,6 @@ const controller = new MetamaskController({
setData, setData,
loadData, loadData,
}) })
const keyringController = controller.keyringController
const txManager = controller.txManager const txManager = controller.txManager
function triggerUi () { function triggerUi () {
if (!popupIsOpen) notification.show() if (!popupIsOpen) notification.show()
@ -81,13 +80,11 @@ function setupControllerConnection (stream) {
stream.pipe(dnode).pipe(stream) stream.pipe(dnode).pipe(stream)
dnode.on('remote', (remote) => { dnode.on('remote', (remote) => {
// push updates to popup // push updates to popup
controller.ethStore.on('update', controller.sendUpdate.bind(controller)) var sendUpdate = remote.sendUpdate.bind(remote)
controller.listeners.push(remote) controller.on('update', sendUpdate)
keyringController.on('update', controller.sendUpdate.bind(controller))
// teardown on disconnect // teardown on disconnect
eos(stream, () => { eos(stream, () => {
controller.ethStore.removeListener('update', controller.sendUpdate.bind(controller)) controller.removeListener('update', sendUpdate)
popupIsOpen = false popupIsOpen = false
}) })
}) })

View File

@ -1,3 +1,4 @@
const EventEmitter = require('events')
const extend = require('xtend') const extend = require('xtend')
const EthStore = require('eth-store') const EthStore = require('eth-store')
const MetaMaskProvider = require('web3-provider-engine/zero.js') const MetaMaskProvider = require('web3-provider-engine/zero.js')
@ -13,12 +14,12 @@ const autoFaucet = require('./lib/auto-faucet')
const nodeify = require('./lib/nodeify') const nodeify = require('./lib/nodeify')
const IdStoreMigrator = require('./lib/idStore-migrator') const IdStoreMigrator = require('./lib/idStore-migrator')
module.exports = class MetamaskController { module.exports = class MetamaskController extends EventEmitter {
constructor (opts) { constructor (opts) {
super()
this.state = { network: 'loading' } this.state = { network: 'loading' }
this.opts = opts this.opts = opts
this.listeners = []
this.configManager = new ConfigManager(opts) this.configManager = new ConfigManager(opts)
this.keyringController = new KeyringController({ this.keyringController = new KeyringController({
configManager: this.configManager, configManager: this.configManager,
@ -62,6 +63,7 @@ module.exports = class MetamaskController {
}) })
this.ethStore.on('update', this.sendUpdate.bind(this)) this.ethStore.on('update', this.sendUpdate.bind(this))
this.keyringController.on('update', this.sendUpdate.bind(this))
} }
getState () { getState () {
@ -165,10 +167,7 @@ module.exports = class MetamaskController {
sendUpdate () { sendUpdate () {
this.getState() this.getState()
.then((state) => { .then((state) => {
this.emit('update', state)
this.listeners.forEach((remote) => {
remote.sendUpdate(state)
})
}) })
} }