You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
7 years ago
|
const ObservableStore = require('obs-store')
|
||
|
const extend = require('xtend')
|
||
|
const BalanceController = require('./balance')
|
||
|
|
||
7 years ago
|
class ComputedbalancesController {
|
||
7 years ago
|
|
||
|
constructor (opts = {}) {
|
||
|
const { ethStore, txController } = opts
|
||
|
this.ethStore = ethStore
|
||
|
this.txController = txController
|
||
|
|
||
|
const initState = extend({
|
||
7 years ago
|
computedBalances: {},
|
||
7 years ago
|
}, opts.initState)
|
||
|
this.store = new ObservableStore(initState)
|
||
7 years ago
|
this.balances = {}
|
||
7 years ago
|
|
||
|
this._initBalanceUpdating()
|
||
|
}
|
||
|
|
||
7 years ago
|
updateAllBalances () {
|
||
|
for (let address in this.balances) {
|
||
|
this.balances[address].updateBalance()
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
_initBalanceUpdating () {
|
||
|
const store = this.ethStore.getState()
|
||
|
this.addAnyAccountsFromStore(store)
|
||
|
this.ethStore.subscribe(this.addAnyAccountsFromStore.bind(this))
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
addAnyAccountsFromStore(store) {
|
||
|
const balances = store.accounts
|
||
7 years ago
|
|
||
7 years ago
|
for (let address in balances) {
|
||
|
this.trackAddressIfNotAlready(address)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
trackAddressIfNotAlready (address) {
|
||
|
const state = this.store.getState()
|
||
|
if (!(address in state.computedBalances)) {
|
||
|
this.trackAddress(address)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
trackAddress (address) {
|
||
|
let updater = new BalanceController({
|
||
|
address,
|
||
|
ethStore: this.ethStore,
|
||
|
txController: this.txController,
|
||
|
})
|
||
|
updater.store.subscribe((accountBalance) => {
|
||
|
let newState = this.store.getState()
|
||
|
newState.computedBalances[address] = accountBalance
|
||
|
this.store.updateState(newState)
|
||
|
})
|
||
7 years ago
|
this.balances[address] = updater
|
||
7 years ago
|
updater.updateBalance()
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
module.exports = ComputedbalancesController
|