Refactor of code into separate reducers and actions.

feature/default_network_editable
Kevin Serrano 8 years ago
parent 08ca7dac5a
commit e7e024bcdd
No known key found for this signature in database
GPG Key ID: 7CC862A58D2889B4
  1. 2
      app/scripts/controllers/preferences.js
  2. 17
      app/scripts/metamask-controller.js
  3. 12
      test/unit/actions/config_test.js
  4. 30
      ui/app/actions.js
  5. 1
      ui/app/reducers/metamask.js

@ -29,7 +29,7 @@ class PreferencesController {
return this.addToFrequentRpcList(_url)
.then((rpcList) => {
this.store.updateState({ frequentRpcList: rpcList })
return rpcList
return Promise.resolve()
})
}

@ -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()

@ -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)
})
})

@ -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,
}
}
}

@ -18,6 +18,7 @@ function reduceMetamask (state, action) {
conversionDate: 'N/A',
noActiveNotices: true,
lastUnreadNotice: undefined,
frequentRpcList: [],
}, state.metamask)
switch (action.type) {

Loading…
Cancel
Save