Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move activation logic into token rates controller #8744

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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()
})
})