From 9c7eafc86f1d9d06dd6a5b66ecd2154b09299d03 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Thu, 19 Apr 2018 14:37:08 -0400 Subject: Add more documentation to computed balances controller --- app/scripts/controllers/computed-balances.js | 37 +++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'app/scripts/controllers/computed-balances.js') diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js index 907b087cf..cf2e05333 100644 --- a/app/scripts/controllers/computed-balances.js +++ b/app/scripts/controllers/computed-balances.js @@ -2,8 +2,16 @@ const ObservableStore = require('obs-store') const extend = require('xtend') const BalanceController = require('./balance') +/** + * Background controller responsible for syncing + * and computing ETH balances for all accounts + */ class ComputedbalancesController { - + /** + * Creates a new controller instance + * + * @param {Object} [opts] Controller configuration parameters + */ constructor (opts = {}) { const { accountTracker, txController, blockTracker } = opts this.accountTracker = accountTracker @@ -19,6 +27,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 +37,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 {Object} store Account tracking state + */ syncAllAccountsFromStore (store) { const upstream = Object.keys(store.accounts) const balances = Object.keys(this.balances) @@ -50,6 +72,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 +86,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, -- cgit From 8636f3bae547ace7d099a3ed516bf013dfe3858e Mon Sep 17 00:00:00 2001 From: bitpshr Date: Thu, 19 Apr 2018 15:12:04 -0400 Subject: Clean up JSDoc for background scripts --- app/scripts/controllers/computed-balances.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'app/scripts/controllers/computed-balances.js') diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js index cf2e05333..1a6802f9a 100644 --- a/app/scripts/controllers/computed-balances.js +++ b/app/scripts/controllers/computed-balances.js @@ -2,6 +2,14 @@ const ObservableStore = require('obs-store') const extend = require('xtend') const BalanceController = require('./balance') +/** + * @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 @@ -10,7 +18,7 @@ class ComputedbalancesController { /** * Creates a new controller instance * - * @param {Object} [opts] Controller configuration parameters + * @param {ComputedBalancesOptions} [opts] Controller configuration parameters */ constructor (opts = {}) { const { accountTracker, txController, blockTracker } = opts @@ -52,7 +60,7 @@ class ComputedbalancesController { * Uses current account state to sync and track all * addresses associated with the current account * - * @param {Object} store Account tracking state + * @param {{ accounts: Object }} store Account tracking state */ syncAllAccountsFromStore (store) { const upstream = Object.keys(store.accounts) -- cgit