tokens related functions refactor

feature/default_network_editable
Esteban MIno 6 years ago
parent ccae79d392
commit 3124e8c612
  1. 37
      app/scripts/controllers/preferences.js

@ -190,13 +190,14 @@ class PreferencesController {
* Setter for the `selectedAddress` property * Setter for the `selectedAddress` property
* *
* @param {string} _address A new hex address for an account * @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) { setSelectedAddress (_address) {
const address = normalizeAddress(_address) const address = normalizeAddress(_address)
const tokens = this._updateTokens(address) this._updateTokens(address)
this.store.updateState({ selectedAddress: address, tokens }) this.store.updateState({ selectedAddress: address })
const tokens = this.store.getState().tokens
return Promise.resolve(tokens) return Promise.resolve(tokens)
} }
@ -392,12 +393,8 @@ class PreferencesController {
* *
*/ */
_subscribeProviderType () { _subscribeProviderType () {
this.network.providerStore.subscribe(({ type }) => { this.network.providerStore.subscribe(() => {
const selectedAddress = this.store.getState().selectedAddress const { tokens } = this._getTokenRelatedStates()
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.store.updateState({ tokens }) this.store.updateState({ tokens })
}) })
} }
@ -409,11 +406,7 @@ class PreferencesController {
* *
*/ */
_updateAccountTokens (tokens) { _updateAccountTokens (tokens) {
const accountTokens = this.store.getState().accountTokens const { accountTokens, providerType, selectedAddress } = this._getTokenRelatedStates()
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] = []
accountTokens[selectedAddress][providerType] = tokens accountTokens[selectedAddress][providerType] = tokens
this.store.updateState({ accountTokens, tokens }) this.store.updateState({ accountTokens, tokens })
} }
@ -421,15 +414,29 @@ class PreferencesController {
/** /**
* Updates `tokens` of current account and network. * Updates `tokens` of current account and network.
* *
* @param {string} selectedAddress Account address to be updated with.
* *
*/ */
_updateTokens (selectedAddress) { _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 const accountTokens = this.store.getState().accountTokens
if (!selectedAddress) selectedAddress = this.store.getState().selectedAddress
const providerType = this.network.providerStore.getState().type const providerType = this.network.providerStore.getState().type
if (!(selectedAddress in accountTokens)) accountTokens[selectedAddress] = {} if (!(selectedAddress in accountTokens)) accountTokens[selectedAddress] = {}
if (!(providerType in accountTokens[selectedAddress])) accountTokens[selectedAddress][providerType] = [] if (!(providerType in accountTokens[selectedAddress])) accountTokens[selectedAddress][providerType] = []
const tokens = accountTokens[selectedAddress][providerType] const tokens = accountTokens[selectedAddress][providerType]
return tokens return { tokens, accountTokens, providerType, selectedAddress }
} }
} }

Loading…
Cancel
Save