2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from 'obs-store'
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} CachedBalancesOptions
|
|
|
|
* @property {Object} accountTracker An {@code AccountTracker} reference
|
|
|
|
* @property {Function} getNetwork A function to get the current network
|
|
|
|
* @property {Object} initState The initial controller state
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background controller responsible for maintaining
|
|
|
|
* a cache of account balances in local storage
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class CachedBalancesController {
|
2018-11-30 23:51:24 +01:00
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {CachedBalancesOptions} [opts] - Controller configuration parameters
|
2018-11-30 23:51:24 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const { accountTracker, getNetwork } = opts
|
|
|
|
|
|
|
|
this.accountTracker = accountTracker
|
|
|
|
this.getNetwork = getNetwork
|
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
const initState = { cachedBalances: {}, ...opts.initState }
|
2018-11-30 23:51:24 +01:00
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
|
|
|
|
this._registerUpdates()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the cachedBalances property for the current network. Cached balances will be updated to those in the passed accounts
|
|
|
|
* if balances in the passed accounts are truthy.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} obj - The the recently updated accounts object for the current network
|
2018-11-30 23:51:24 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async updateCachedBalances({ accounts }) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const network = await this.getNetwork()
|
2020-11-03 00:41:28 +01:00
|
|
|
const balancesToCache = await this._generateBalancesToCache(
|
|
|
|
accounts,
|
|
|
|
network,
|
|
|
|
)
|
2018-11-30 23:51:24 +01:00
|
|
|
this.store.updateState({
|
|
|
|
cachedBalances: balancesToCache,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_generateBalancesToCache(newAccounts, currentNetwork) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const { cachedBalances } = this.store.getState()
|
|
|
|
const currentNetworkBalancesToCache = { ...cachedBalances[currentNetwork] }
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
Object.keys(newAccounts).forEach((accountID) => {
|
2018-11-30 23:51:24 +01:00
|
|
|
const account = newAccounts[accountID]
|
|
|
|
|
|
|
|
if (account.balance) {
|
|
|
|
currentNetworkBalancesToCache[accountID] = account.balance
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const balancesToCache = {
|
|
|
|
...cachedBalances,
|
|
|
|
[currentNetwork]: currentNetworkBalancesToCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
return balancesToCache
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:09:38 +02:00
|
|
|
/**
|
|
|
|
* Removes cachedBalances
|
|
|
|
*/
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
clearCachedBalances() {
|
2020-07-17 04:09:38 +02:00
|
|
|
this.store.updateState({ cachedBalances: {} })
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:51:24 +01:00
|
|
|
/**
|
|
|
|
* Sets up listeners and subscriptions which should trigger an update of cached balances. These updates will
|
|
|
|
* happen when the current account changes. Which happens on block updates, as well as on network and account
|
|
|
|
* selections.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_registerUpdates() {
|
2018-11-30 23:51:24 +01:00
|
|
|
const update = this.updateCachedBalances.bind(this)
|
|
|
|
this.accountTracker.store.subscribe(update)
|
|
|
|
}
|
|
|
|
}
|