diff options
author | Chi Kei Chan <chikeichan@gmail.com> | 2017-10-21 02:05:10 +0800 |
---|---|---|
committer | Chi Kei Chan <chikeichan@gmail.com> | 2017-10-21 02:05:10 +0800 |
commit | 344b467d03efd3f6fb50f11f4a5b16673e0d8bfd (patch) | |
tree | b7554db3427e378a6107edd45eb93a78fdc24dfe /app/scripts/controllers | |
parent | 79be956be9f5297d6d601941e50d5ae4eca58560 (diff) | |
parent | e3a7da961dd01bcb936a000cdf62e2290a5024ef (diff) | |
download | tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar.gz tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar.bz2 tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar.lz tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar.xz tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.tar.zst tangerine-wallet-browser-344b467d03efd3f6fb50f11f4a5b16673e0d8bfd.zip |
Merge branch 'master' into NewUI-flat
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r-- | app/scripts/controllers/balance.js | 10 | ||||
-rw-r--r-- | app/scripts/controllers/computed-balances.js | 23 |
2 files changed, 27 insertions, 6 deletions
diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js index 4fa4c78fe..f83f294cc 100644 --- a/app/scripts/controllers/balance.js +++ b/app/scripts/controllers/balance.js @@ -5,7 +5,9 @@ const BN = require('ethereumjs-util').BN class BalanceController { constructor (opts = {}) { + this._validateParams(opts) const { address, accountTracker, txController, blockTracker } = opts + this.address = address this.accountTracker = accountTracker this.txController = txController @@ -65,6 +67,14 @@ class BalanceController { return pending } + _validateParams (opts) { + const { address, accountTracker, txController, blockTracker } = opts + if (!address || !accountTracker || !txController || !blockTracker) { + const error = 'Cannot construct a balance checker without address, accountTracker, txController, and blockTracker.' + throw new Error(error) + } + } + } module.exports = BalanceController diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js index 2479e1b3a..9855f715e 100644 --- a/app/scripts/controllers/computed-balances.js +++ b/app/scripts/controllers/computed-balances.js @@ -20,23 +20,34 @@ class ComputedbalancesController { } updateAllBalances () { - for (let address in this.accountTracker.store.getState().accounts) { + Object.keys(this.balances).forEach((balance) => { + const address = balance.address this.balances[address].updateBalance() - } + }) } _initBalanceUpdating () { const store = this.accountTracker.store.getState() - this.addAnyAccountsFromStore(store) - this.accountTracker.store.subscribe(this.addAnyAccountsFromStore.bind(this)) + this.syncAllAccountsFromStore(store) + this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this)) } - addAnyAccountsFromStore(store) { - const balances = store.accounts + 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) { |