From 75177ce34cac589be26fb8089aac04feccdbae81 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 18 Oct 2017 15:08:34 -0700 Subject: Make account tracking more reactive We were doing a lot of conditional observation & updating. Pulled out a bunch of that for generic observer/syncers. --- app/scripts/controllers/computed-balances.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js index 3479eae2b..009405d29 100644 --- a/app/scripts/controllers/computed-balances.js +++ b/app/scripts/controllers/computed-balances.js @@ -25,22 +25,28 @@ class ComputedbalancesController { } } - forgetAllBalances () { - this.balances = {} - } - _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) { -- cgit