mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
75177ce34c
We were doing a lot of conditional observation & updating. Pulled out a bunch of that for generic observer/syncers.
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
const ObservableStore = require('obs-store')
|
|
const extend = require('xtend')
|
|
const BalanceController = require('./balance')
|
|
|
|
class ComputedbalancesController {
|
|
|
|
constructor (opts = {}) {
|
|
const { accountTracker, txController, blockTracker } = opts
|
|
this.accountTracker = accountTracker
|
|
this.txController = txController
|
|
this.blockTracker = blockTracker
|
|
|
|
const initState = extend({
|
|
computedBalances: {},
|
|
}, opts.initState)
|
|
this.store = new ObservableStore(initState)
|
|
this.balances = {}
|
|
|
|
this._initBalanceUpdating()
|
|
}
|
|
|
|
updateAllBalances () {
|
|
for (let address in this.accountTracker.store.getState().accounts) {
|
|
this.balances[address].updateBalance()
|
|
}
|
|
}
|
|
|
|
_initBalanceUpdating () {
|
|
const store = this.accountTracker.store.getState()
|
|
this.syncAllAccountsFromStore(store)
|
|
this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
|
|
}
|
|
|
|
syncAllAccountsFromStore(store) {
|
|
const upstream = Object.keys(store.accounts)
|
|
const balances = Object.keys(this.balances)
|
|
.map(address => this.balances[address])
|
|
|
|
// Follow new addresses
|
|
for (let address in balances) {
|
|
this.trackAddressIfNotAlready(address)
|
|
}
|
|
|
|
// Unfollow old ones
|
|
balances.forEach(({ address }) => {
|
|
if (!upstream.includes(address)) {
|
|
delete this.balances[address]
|
|
}
|
|
})
|
|
}
|
|
|
|
trackAddressIfNotAlready (address) {
|
|
const state = this.store.getState()
|
|
if (!(address in state.computedBalances)) {
|
|
this.trackAddress(address)
|
|
}
|
|
}
|
|
|
|
trackAddress (address) {
|
|
let updater = new BalanceController({
|
|
address,
|
|
accountTracker: this.accountTracker,
|
|
txController: this.txController,
|
|
blockTracker: this.blockTracker,
|
|
})
|
|
updater.store.subscribe((accountBalance) => {
|
|
let newState = this.store.getState()
|
|
newState.computedBalances[address] = accountBalance
|
|
this.store.updateState(newState)
|
|
})
|
|
this.balances[address] = updater
|
|
updater.updateBalance()
|
|
}
|
|
}
|
|
|
|
module.exports = ComputedbalancesController
|