diff --git a/app/scripts/controllers/token-rates.js b/app/scripts/controllers/token-rates.js index b5b40f0af..6724ede3d 100644 --- a/app/scripts/controllers/token-rates.js +++ b/app/scripts/controllers/token-rates.js @@ -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 } /** @@ -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} */ @@ -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) + } } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f36da612e..7787601a5 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -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() } }) @@ -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, @@ -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 * diff --git a/test/unit/app/controllers/token-rates-controller.js b/test/unit/app/controllers/token-rates-controller.js index edcc74120..8802c5c6e 100644 --- a/test/unit/app/controllers/token-rates-controller.js +++ b/test/unit/app/controllers/token-rates-controller.js @@ -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() }) })