1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00

Add better event lifecycle management to token list.

Token list now renders errors when a token lookup fails.
Also now cleans up event listeners when re-initializing the token list.
This commit is contained in:
Dan Finlay 2017-06-19 19:11:55 -07:00
parent 0799e5edf5
commit a2781df8b4

View File

@ -24,7 +24,7 @@ function TokenList () {
TokenList.prototype.render = function () {
const state = this.state
const { tokens, isLoading } = state
const { tokens, isLoading, error } = state
const { userAddress } = this.props
@ -32,6 +32,11 @@ TokenList.prototype.render = function () {
return this.message('Loading')
}
if (error) {
log.error(error)
return this.message('There was a problem loading your token balances.')
}
const network = this.props.network
const tokenViews = tokens.map((tokenData) => {
@ -85,7 +90,10 @@ TokenList.prototype.componentDidMount = function () {
TokenList.prototype.createFreshTokenTracker = function () {
if (this.tracker) {
// Clean up old trackers when refreshing:
this.tracker.stop()
this.tracker.removeListener('update', this.balanceUpdater)
this.tracker.removeListener('error', this.showError)
}
if (!global.ethereumProvider) return
@ -97,9 +105,15 @@ TokenList.prototype.createFreshTokenTracker = function () {
pollingInterval: 8000,
})
this.tracker.on('update', (tokenData) => {
this.updateBalances(tokenData)
})
// Set up listener instances for cleaning up
this.balanceUpdater = this.updateBalances.bind(this)
this.showError = (error) => {
this.setState({ error, isLoading: false })
}
this.tracker.on('update', this.balanceUpdater)
this.tracker.on('error', this.showError)
this.tracker.updateBalances()
.then(() => {
this.updateBalances(this.tracker.serialize())