aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/computed-balances.js
diff options
context:
space:
mode:
authorThomas <thomas.b.huang@gmail.com>2018-05-17 13:44:51 +0800
committerThomas <thomas.b.huang@gmail.com>2018-05-17 13:44:51 +0800
commitd9be7f989a86e3fdfd83e4c632fd08cefd8309e5 (patch)
treebbfeb7098997244ce7e8ce28e896faa2dfb6bb6e /app/scripts/controllers/computed-balances.js
parent06e25205b200b976e286c670cc5e703439dab05c (diff)
parentf441153211c7920573f8bfb699bfda1b6de7efe9 (diff)
downloadtangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.gz
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.zst
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.zip
Merge branch 'testing' of https://github.com/tmashuang/metamask-extension into testing
Diffstat (limited to 'app/scripts/controllers/computed-balances.js')
-rw-r--r--app/scripts/controllers/computed-balances.js45
1 files changed, 44 insertions, 1 deletions
diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js
index 907b087cf..1a6802f9a 100644
--- a/app/scripts/controllers/computed-balances.js
+++ b/app/scripts/controllers/computed-balances.js
@@ -2,8 +2,24 @@ const ObservableStore = require('obs-store')
const extend = require('xtend')
const BalanceController = require('./balance')
-class ComputedbalancesController {
+/**
+ * @typedef {Object} ComputedBalancesOptions
+ * @property {Object} accountTracker Account tracker store reference
+ * @property {Object} txController Token controller reference
+ * @property {Object} blockTracker Block tracker reference
+ * @property {Object} initState Initial state to populate this internal store with
+ */
+/**
+ * Background controller responsible for syncing
+ * and computing ETH balances for all accounts
+ */
+class ComputedbalancesController {
+ /**
+ * Creates a new controller instance
+ *
+ * @param {ComputedBalancesOptions} [opts] Controller configuration parameters
+ */
constructor (opts = {}) {
const { accountTracker, txController, blockTracker } = opts
this.accountTracker = accountTracker
@@ -19,6 +35,9 @@ class ComputedbalancesController {
this._initBalanceUpdating()
}
+ /**
+ * Updates balances associated with each internal address
+ */
updateAllBalances () {
Object.keys(this.balances).forEach((balance) => {
const address = balance.address
@@ -26,12 +45,23 @@ class ComputedbalancesController {
})
}
+ /**
+ * Initializes internal address tracking
+ *
+ * @private
+ */
_initBalanceUpdating () {
const store = this.accountTracker.store.getState()
this.syncAllAccountsFromStore(store)
this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
}
+ /**
+ * Uses current account state to sync and track all
+ * addresses associated with the current account
+ *
+ * @param {{ accounts: Object }} store Account tracking state
+ */
syncAllAccountsFromStore (store) {
const upstream = Object.keys(store.accounts)
const balances = Object.keys(this.balances)
@@ -50,6 +80,13 @@ class ComputedbalancesController {
})
}
+ /**
+ * Conditionally establishes a new subscription
+ * to track an address associated with the current
+ * account
+ *
+ * @param {string} address Address to conditionally subscribe to
+ */
trackAddressIfNotAlready (address) {
const state = this.store.getState()
if (!(address in state.computedBalances)) {
@@ -57,6 +94,12 @@ class ComputedbalancesController {
}
}
+ /**
+ * Establishes a new subscription to track an
+ * address associated with the current account
+ *
+ * @param {string} address Address to conditionally subscribe to
+ */
trackAddress (address) {
const updater = new BalanceController({
address,