Skip to content

Commit

Permalink
move activation logic into token rates controller
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker committed Jun 5, 2020
1 parent 591d84d commit dffb1a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
31 changes: 16 additions & 15 deletions app/scripts/controllers/token-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ export default class TokenRatesController {
*
* @param {Object} [config] - Options to configure controller
*/
constructor ({ interval = DEFAULT_INTERVAL, currency, preferences } = {}) {
constructor ({ currency, preferences } = {}) {
this.store = new ObservableStore()
this.currency = currency
this.preferences = preferences
this.interval = interval
}

/**
Expand Down Expand Up @@ -50,19 +49,6 @@ export default class TokenRatesController {
this.store.putState({ contractExchangeRates })
}

/**
* @type {Number}
*/
set interval (interval) {
this._handle && clearInterval(this._handle)
if (!interval) {
return
}
this._handle = setInterval(() => {
this.updateExchangeRates()
}, interval)
}

/**
* @type {Object}
*/
Expand All @@ -85,4 +71,19 @@ export default class TokenRatesController {
this._tokens = tokens
this.updateExchangeRates()
}

start (interval = DEFAULT_INTERVAL) {
this._handle && clearInterval(this._handle)
if (!interval) {
return
}
this._handle = setInterval(() => {
this.updateExchangeRates()
}, interval)
this.updateExchangeRates()
}

stop () {
this._handle && clearInterval(this._handle)
}
}
17 changes: 2 additions & 15 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ export default class MetamaskController extends EventEmitter {
if (activeControllerConnections > 0) {
this.accountTracker.start()
this.incomingTransactionsController.start()
this.tokenRatesController.start()
} else {
this.accountTracker.stop()
this.incomingTransactionsController.stop()
this.tokenRatesController.stop()
}
})

Expand Down Expand Up @@ -281,10 +283,6 @@ export default class MetamaskController extends EventEmitter {
this.encryptionPublicKeyManager = new EncryptionPublicKeyManager()
this.typedMessageManager = new TypedMessageManager({ networkController: this.networkController })

// ensure isClientOpenAndUnlocked is updated when memState updates
this.on('update', (memState) => {
this.isClientOpenAndUnlocked = memState.isUnlocked && this._isClientOpen
})

this.store.updateStructure({
AppStateController: this.appStateController.store,
Expand Down Expand Up @@ -2032,20 +2030,9 @@ export default class MetamaskController extends EventEmitter {
*/
set isClientOpen (open) {
this._isClientOpen = open
this.isClientOpenAndUnlocked = this.isUnlocked() && open
this.detectTokensController.isOpen = open
}

/**
* A method for activating the retrieval of price data,
* which should only be fetched when the UI is visible.
* @private
* @param {boolean} active - True if price data should be getting fetched.
*/
set isClientOpenAndUnlocked (active) {
this.tokenRatesController.isActive = active
}

/**
* Creates RPC engine middleware for processing eth_signTypedData requests
*
Expand Down
5 changes: 4 additions & 1 deletion test/unit/app/controllers/token-rates-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ describe('TokenRatesController', function () {

it('should poll on correct interval', async function () {
const stub = sinon.stub(global, 'setInterval')
new TokenRatesController({ interval: 1337 }) // eslint-disable-line no-new
const rateController = new TokenRatesController() // eslint-disable-line no-new
rateController.start(1337)

assert.strictEqual(stub.getCall(0).args[1], 1337)
stub.restore()
rateController.stop()
})
})

0 comments on commit dffb1a1

Please sign in to comment.