blob: cf3c8a7579a8377ea141d99dc4fb86611a311bd8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const extend = require('xtend')
const BalanceController = require('./balance')
class BalancesController {
constructor (opts = {}) {
const { ethStore, txController } = opts
this.ethStore = ethStore
this.txController = txController
const initState = extend({
computedBalances: {},
}, opts.initState)
this.store = new ObservableStore(initState)
this._initBalanceUpdating()
}
_initBalanceUpdating () {
const store = this.ethStore.getState()
this.addAnyAccountsFromStore(store)
this.ethStore.subscribe(this.addAnyAccountsFromStore.bind(this))
}
addAnyAccountsFromStore(store) {
const balances = store.accounts
for (let address in balances) {
this.trackAddressIfNotAlready(address)
}
}
trackAddressIfNotAlready (address) {
const state = this.store.getState()
if (!(address in state.computedBalances)) {
this.trackAddress(address)
}
}
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)
})
updater.updateBalance()
}
}
module.exports = BalancesController
|