2017-01-30 21:42:24 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
2017-02-21 23:25:47 +01:00
|
|
|
const normalizeAddress = require('eth-sig-util').normalize
|
2017-02-23 00:12:56 +01:00
|
|
|
const extend = require('xtend')
|
2017-01-30 21:42:24 +01:00
|
|
|
|
|
|
|
class PreferencesController {
|
|
|
|
|
|
|
|
constructor (opts = {}) {
|
2017-03-09 22:07:38 +01:00
|
|
|
const initState = extend({
|
|
|
|
frequentRpcList: [],
|
|
|
|
}, opts.initState)
|
2017-01-30 21:42:24 +01:00
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// PUBLIC METHODS
|
|
|
|
//
|
|
|
|
|
2017-02-21 21:32:13 +01:00
|
|
|
setSelectedAddress (_address) {
|
2017-01-31 00:08:31 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const address = normalizeAddress(_address)
|
|
|
|
this.store.updateState({ selectedAddress: address })
|
|
|
|
resolve()
|
|
|
|
})
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 21:32:13 +01:00
|
|
|
getSelectedAddress (_address) {
|
2017-01-30 21:42:24 +01:00
|
|
|
return this.store.getState().selectedAddress
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:58 +01:00
|
|
|
updateFrequentRpcList (_url) {
|
|
|
|
return this.addToFrequentRpcList(_url)
|
|
|
|
.then((rpcList) => {
|
|
|
|
this.store.updateState({ frequentRpcList: rpcList })
|
2017-03-08 01:01:51 +01:00
|
|
|
return Promise.resolve()
|
2017-02-23 22:56:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:51:46 +01:00
|
|
|
addToFrequentRpcList (_url) {
|
2017-02-23 00:12:56 +01:00
|
|
|
let rpcList = this.getFrequentRpcList()
|
|
|
|
let index = rpcList.findIndex((element) => { return element === _url })
|
|
|
|
if (index !== -1) {
|
|
|
|
rpcList.splice(index, 1)
|
|
|
|
}
|
|
|
|
if (_url !== 'http://localhost:8545') {
|
2017-02-21 21:32:13 +01:00
|
|
|
rpcList.push(_url)
|
2017-02-23 00:12:56 +01:00
|
|
|
}
|
|
|
|
if (rpcList.length > 2) {
|
|
|
|
rpcList.shift()
|
|
|
|
}
|
2017-02-23 22:56:58 +01:00
|
|
|
return Promise.resolve(rpcList)
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 21:51:46 +01:00
|
|
|
getFrequentRpcList () {
|
2017-02-23 00:12:56 +01:00
|
|
|
return this.store.getState().frequentRpcList
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:42:24 +01:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
|
|
|
|
2017-02-23 22:56:58 +01:00
|
|
|
|
|
|
|
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PreferencesController
|