diff options
author | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2018-07-21 09:50:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-21 09:50:49 +0800 |
commit | c5520de1159718466ab846add99e0fea1da1cb06 (patch) | |
tree | c44f3a009ea4bc42984f122fd937a8a7622ef73f | |
parent | e094d4ad1fb84a9bc663c328d0650bd9d8bf8716 (diff) | |
parent | 9be22775c31699b49873a43820dc315067a567a9 (diff) | |
download | tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar.gz tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar.bz2 tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar.lz tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar.xz tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.tar.zst tangerine-wallet-browser-c5520de1159718466ab846add99e0fea1da1cb06.zip |
Merge pull request #4844 from MetaMask/detectTokenFeature
Detect token feature
-rw-r--r-- | app/scripts/controllers/detect-tokens.js | 17 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 2 | ||||
-rw-r--r-- | test/unit/app/controllers/detect-tokens-test.js | 33 |
3 files changed, 40 insertions, 12 deletions
diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index f1810cfa1..b30dc00f1 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -64,10 +64,9 @@ class DetectTokensController { * */ restartTokenDetection () { - if (this.isActive && this.selectedAddress) { - this.detectNewTokens() - this.interval = DEFAULT_INTERVAL - } + if (!(this.isActive && this.selectedAddress)) { return } + this.detectNewTokens() + this.interval = DEFAULT_INTERVAL } /** @@ -113,11 +112,19 @@ class DetectTokensController { this._keyringMemStore = keyringMemStore this._keyringMemStore.subscribe(({ isUnlocked }) => { if (this.isUnlocked !== isUnlocked) { - if (isUnlocked) { this.restartTokenDetection() } this.isUnlocked = isUnlocked + if (isUnlocked) { this.restartTokenDetection() } } }) } + + /** + * Internal isActive state + * @type {Object} + */ + get isActive () { + return this.isOpen && this.isUnlocked + } } module.exports = DetectTokensController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index e629d1359..3b303a95c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1432,6 +1432,7 @@ module.exports = class MetamaskController extends EventEmitter { set isClientOpen (open) { this._isClientOpen = open this.isClientOpenAndUnlocked = this.getState().isUnlocked && open + this.detectTokensController.isOpen = open } /** @@ -1442,6 +1443,5 @@ module.exports = class MetamaskController extends EventEmitter { */ set isClientOpenAndUnlocked (active) { this.tokenRatesController.isActive = active - this.detectTokensController.isActive = active } } diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index dcb3c431f..426ffe23a 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -29,7 +29,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectNewTokens') @@ -48,7 +49,8 @@ describe('DetectTokensController', () => { network.setProviderType('rinkeby') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) @@ -63,7 +65,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') @@ -82,7 +85,8 @@ describe('DetectTokensController', () => { const preferences = new PreferencesController() preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') @@ -100,7 +104,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectNewTokens') await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388') sandbox.assert.called(stub) @@ -111,10 +116,26 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true controller.selectedAddress = '0x0' var stub = sandbox.stub(controller, 'detectNewTokens') await controller._keyringMemStore.updateState({ isUnlocked: true }) sandbox.assert.called(stub) }) + + it('should not trigger detect new tokens when not open or not unlocked', async () => { + const network = new NetworkController() + network.setProviderType('mainnet') + const preferences = new PreferencesController() + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) + controller.isOpen = true + controller.isUnlocked = false + var stub = sandbox.stub(controller, 'detectTokenBalance') + clock.tick(180000) + sandbox.assert.notCalled(stub) + controller.isOpen = false + controller.isUnlocked = true + clock.tick(180000) + sandbox.assert.notCalled(stub) + }) }) |