From e7e024bcdd2c93b34a0baf8bc27e6c571c098476 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Tue, 7 Mar 2017 16:01:51 -0800 Subject: [PATCH] Refactor of code into separate reducers and actions. --- app/scripts/controllers/preferences.js | 2 +- app/scripts/metamask-controller.js | 17 +++++++++++---- test/unit/actions/config_test.js | 12 ++--------- ui/app/actions.js | 30 ++++++++------------------ ui/app/reducers/metamask.js | 1 + 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 8fd5fb8a0..18fccf11b 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -29,7 +29,7 @@ class PreferencesController { return this.addToFrequentRpcList(_url) .then((rpcList) => { this.store.updateState({ frequentRpcList: rpcList }) - return rpcList + return Promise.resolve() }) } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f6dbf899d..3b3329b58 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -244,7 +244,8 @@ module.exports = class MetamaskController extends EventEmitter { return { // etc getState: (cb) => cb(null, this.getState()), - setRpcTarget: this.setRpcTarget.bind(this), + setDefaultRpc: this.setDefaultRpc.bind(this), + setCustomRpc: this.setCustomRpc.bind(this), setProviderType: this.setProviderType.bind(this), useEtherscanProvider: this.useEtherscanProvider.bind(this), setCurrentCurrency: this.setCurrentCurrency.bind(this), @@ -265,7 +266,6 @@ module.exports = class MetamaskController extends EventEmitter { // PreferencesController setSelectedAddress: nodeify(preferencesController.setSelectedAddress).bind(preferencesController), - updateFrequentRpcList: nodeify(preferencesController.updateFrequentRpcList).bind(preferencesController), // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), @@ -662,12 +662,21 @@ module.exports = class MetamaskController extends EventEmitter { if (this.isNetworkLoading()) this.lookupNetwork() } - setRpcTarget (rpcTarget) { - this.configManager.setRpcTarget(rpcTarget) + setDefaultRpc () { + this.configManager.setRpcTarget('http://localhost:8545') extension.runtime.reload() this.lookupNetwork() } + setCustomRpc (rpcTarget, rpcList) { + this.configManager.setRpcTarget(rpcTarget) + return this.preferencesController.updateFrequentRpcList(rpcTarget) + .then(() => { + extension.runtime.reload() + this.lookupNetwork() + }) + } + setProviderType (type) { this.configManager.setProviderType(type) extension.runtime.reload() diff --git a/test/unit/actions/config_test.js b/test/unit/actions/config_test.js index eca2f7c8f..02704cb42 100644 --- a/test/unit/actions/config_test.js +++ b/test/unit/actions/config_test.js @@ -36,16 +36,10 @@ describe ('config view actions', function() { value: 'foo', } - const secondAction = { - type: actions.SET_RPC_LIST, - value: ['foo'], - } - var result = reducers(initialState, action) result = reducers(result, secondAction) assert.equal(result.metamask.provider.type, 'rpc') assert.equal(result.metamask.provider.rpcTarget, 'foo') - assert.equal(result.metamask.frequentRpcList[0], 'foo') }) it('should handle multiple requests to change the rpc gracefully', function() { @@ -60,10 +54,8 @@ describe ('config view actions', function() { } var result = reducers(initialState, action) - var secondResult = reducers(result, secondAction) - var thirdResult = reducers(secondResult, action) - var fourthResult = reducers(thirdResult, secondAction) - assert.equal(fourthResult.metamask.frequentRpcList.length, 1) + var secondResult = reducers(result, action) + assert.equal(secondResult.metamask.frequentRpcList.length, 1) }) }) diff --git a/ui/app/actions.js b/ui/app/actions.js index 4172ea5df..2fc0d3523 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -674,33 +674,21 @@ function markAccountsFound() { // default rpc target refers to localhost:8545 in this instance. function setDefaultRpcTarget (rpcList) { log.debug(`background.setDefaultRpcTarget`) - background.setRpcTarget('http://localhost:8545') - return (dispatch) => { - dispatch({ - type: actions.SET_RPC_TARGET, - value: 'http://localhost:8545', - }) - dispatch({ - type: actions.SET_RPC_LIST, - value: rpcList, - }) + background.setDefaultRpc() + return { + type: actions.SET_RPC_TARGET, + value: 'http://localhost:8545', } } function setRpcTarget (newRpc) { return (dispatch) => { log.debug(`background.setRpcTarget`) - background.setRpcTarget(newRpc) - background.updateFrequentRpcList(newRpc, (rpcList) => { - dispatch({ - type: actions.SET_RPC_TARGET, - value: newRpc, - }) - dispatch({ - type: actions.SET_RPC_LIST, - value: rpcList, - }) - }) + background.setCustomRpc(newRpc) + return { + type: actions.SET_RPC_TARGET, + value: newRpc, + } } } diff --git a/ui/app/reducers/metamask.js b/ui/app/reducers/metamask.js index c09556c91..a3c07d977 100644 --- a/ui/app/reducers/metamask.js +++ b/ui/app/reducers/metamask.js @@ -18,6 +18,7 @@ function reduceMetamask (state, action) { conversionDate: 'N/A', noActiveNotices: true, lastUnreadNotice: undefined, + frequentRpcList: [], }, state.metamask) switch (action.type) {