Merge pull request #1147 from MetaMask/i1144-moarrpc
Save recently used RPCsfeature/default_network_editable
commit
26cac57838
@ -0,0 +1,63 @@ |
||||
const ObservableStore = require('obs-store') |
||||
const normalizeAddress = require('eth-sig-util').normalize |
||||
const extend = require('xtend') |
||||
|
||||
class PreferencesController { |
||||
|
||||
constructor (opts = {}) { |
||||
const initState = extend({ frequentRpcList: [] }, opts.initState) |
||||
this.store = new ObservableStore(initState) |
||||
} |
||||
|
||||
//
|
||||
// PUBLIC METHODS
|
||||
//
|
||||
|
||||
setSelectedAddress (_address) { |
||||
return new Promise((resolve, reject) => { |
||||
const address = normalizeAddress(_address) |
||||
this.store.updateState({ selectedAddress: address }) |
||||
resolve() |
||||
}) |
||||
} |
||||
|
||||
getSelectedAddress (_address) { |
||||
return this.store.getState().selectedAddress |
||||
} |
||||
|
||||
updateFrequentRpcList (_url) { |
||||
return this.addToFrequentRpcList(_url) |
||||
.then((rpcList) => { |
||||
this.store.updateState({ frequentRpcList: rpcList }) |
||||
return Promise.resolve() |
||||
}) |
||||
} |
||||
|
||||
addToFrequentRpcList (_url) { |
||||
let rpcList = this.getFrequentRpcList() |
||||
let index = rpcList.findIndex((element) => { return element === _url }) |
||||
if (index !== -1) { |
||||
rpcList.splice(index, 1) |
||||
} |
||||
if (_url !== 'http://localhost:8545') { |
||||
rpcList.push(_url) |
||||
} |
||||
if (rpcList.length > 2) { |
||||
rpcList.shift() |
||||
} |
||||
return Promise.resolve(rpcList) |
||||
} |
||||
|
||||
getFrequentRpcList () { |
||||
return this.store.getState().frequentRpcList |
||||
} |
||||
|
||||
//
|
||||
// PRIVATE METHODS
|
||||
//
|
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
module.exports = PreferencesController |
@ -1,33 +0,0 @@ |
||||
const ObservableStore = require('obs-store') |
||||
const normalizeAddress = require('eth-sig-util').normalize |
||||
|
||||
class PreferencesController { |
||||
|
||||
constructor (opts = {}) { |
||||
const initState = opts.initState || {} |
||||
this.store = new ObservableStore(initState) |
||||
} |
||||
|
||||
//
|
||||
// PUBLIC METHODS
|
||||
//
|
||||
|
||||
setSelectedAddress(_address) { |
||||
return new Promise((resolve, reject) => { |
||||
const address = normalizeAddress(_address) |
||||
this.store.updateState({ selectedAddress: address }) |
||||
resolve() |
||||
}) |
||||
} |
||||
|
||||
getSelectedAddress(_address) { |
||||
return this.store.getState().selectedAddress |
||||
} |
||||
|
||||
//
|
||||
// PRIVATE METHODS
|
||||
//
|
||||
|
||||
} |
||||
|
||||
module.exports = PreferencesController |
Loading…
Reference in new issue