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
|
|
|
|
*/
|
|
|
|
class CachedBalancesController {
|
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
|
|
|
* @param {CachedBalancesOptions} [opts] Controller configuration parameters
|
|
|
|
*/
|
|
|
|
constructor (opts = {}) {
|
|
|
|
const { accountTracker, getNetwork } = opts
|
|
|
|
|
|
|
|
this.accountTracker = accountTracker
|
|
|
|
this.getNetwork = getNetwork
|
|
|
|
|
2020-01-13 19:59:36 +01:00
|
|
|
const initState = Object.assign({
|
2018-11-30 23:51:24 +01:00
|
|
|
cachedBalances: {},
|
|
|
|
}, opts.initState)
|
|
|
|
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>}
|
|
|
|
*/
|
|
|
|
async updateCachedBalances ({ accounts }) {
|
|
|
|
const network = await this.getNetwork()
|
|
|
|
const balancesToCache = await this._generateBalancesToCache(accounts, network)
|
|
|
|
this.store.updateState({
|
|
|
|
cachedBalances: balancesToCache,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_generateBalancesToCache (newAccounts, currentNetwork) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_registerUpdates () {
|
|
|
|
const update = this.updateCachedBalances.bind(this)
|
|
|
|
this.accountTracker.store.subscribe(update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default CachedBalancesController
|