aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts
diff options
context:
space:
mode:
authorEsteban MIno <efmino@uc.cl>2018-08-01 03:59:19 +0800
committerEsteban MIno <efmino@uc.cl>2018-08-01 03:59:19 +0800
commit3124e8c61209af960bc36d23677a8fcb5a4f7943 (patch)
tree1f4ac078cd210ac9a05d64402f7a8509e68735c1 /app/scripts
parentccae79d392b76d6277bbbb7c887f520e66f81a2f (diff)
downloadtangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar.gz
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar.bz2
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar.lz
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar.xz
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.tar.zst
tangerine-wallet-browser-3124e8c61209af960bc36d23677a8fcb5a4f7943.zip
tokens related functions refactor
Diffstat (limited to 'app/scripts')
-rw-r--r--app/scripts/controllers/preferences.js37
1 files changed, 22 insertions, 15 deletions
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index 0cc61ba61..fcbf9c186 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -190,13 +190,14 @@ class PreferencesController {
* Setter for the `selectedAddress` property
*
* @param {string} _address A new hex address for an account
- * @returns {Promise<void>} Promise resolves with undefined
+ * @returns {Promise<void>} Promise resolves with tokens
*
*/
setSelectedAddress (_address) {
const address = normalizeAddress(_address)
- const tokens = this._updateTokens(address)
- this.store.updateState({ selectedAddress: address, tokens })
+ this._updateTokens(address)
+ this.store.updateState({ selectedAddress: address })
+ const tokens = this.store.getState().tokens
return Promise.resolve(tokens)
}
@@ -392,12 +393,8 @@ class PreferencesController {
*
*/
_subscribeProviderType () {
- this.network.providerStore.subscribe(({ type }) => {
- const selectedAddress = this.store.getState().selectedAddress
- const accountTokens = this.store.getState().accountTokens
- if (!(selectedAddress in accountTokens)) accountTokens[selectedAddress] = {}
- if (!(type in accountTokens[selectedAddress])) accountTokens[selectedAddress][type] = []
- const tokens = accountTokens[selectedAddress][type]
+ this.network.providerStore.subscribe(() => {
+ const { tokens } = this._getTokenRelatedStates()
this.store.updateState({ tokens })
})
}
@@ -409,11 +406,7 @@ class PreferencesController {
*
*/
_updateAccountTokens (tokens) {
- const accountTokens = this.store.getState().accountTokens
- const selectedAddress = this.store.getState().selectedAddress
- const providerType = this.network.providerStore.getState().type
- if (!(selectedAddress in accountTokens)) accountTokens[selectedAddress] = {}
- if (!(providerType in accountTokens[selectedAddress])) accountTokens[selectedAddress][providerType] = []
+ const { accountTokens, providerType, selectedAddress } = this._getTokenRelatedStates()
accountTokens[selectedAddress][providerType] = tokens
this.store.updateState({ accountTokens, tokens })
}
@@ -421,15 +414,29 @@ class PreferencesController {
/**
* Updates `tokens` of current account and network.
*
+ * @param {string} selectedAddress Account address to be updated with.
*
*/
_updateTokens (selectedAddress) {
+ const { tokens } = this._getTokenRelatedStates(selectedAddress)
+ this.store.updateState({ tokens })
+ }
+
+ /**
+ * A getter for `tokens` and `accountTokens` related states.
+ *
+ * @param {string} selectedAddress A new hex address for an account
+ * @returns {array, object, string, string} States to interact with tokens in `accountTokens`
+ *
+ */
+ _getTokenRelatedStates (selectedAddress) {
const accountTokens = this.store.getState().accountTokens
+ if (!selectedAddress) selectedAddress = this.store.getState().selectedAddress
const providerType = this.network.providerStore.getState().type
if (!(selectedAddress in accountTokens)) accountTokens[selectedAddress] = {}
if (!(providerType in accountTokens[selectedAddress])) accountTokens[selectedAddress][providerType] = []
const tokens = accountTokens[selectedAddress][providerType]
- return tokens
+ return { tokens, accountTokens, providerType, selectedAddress }
}
}