aboutsummaryrefslogblamecommitdiffstats
path: root/app/scripts/lib/controllers/preferences.js
blob: 8cc3201791f0887808bcc0c5e9a4e5e061132731 (plain) (tree)
1
2
3
4
5
6
7
8

                                                         
                               



                             
                                                                     






                                               
                                 




                                                          

   
                                 


                                                
                               





                                                                           
                        





                                                        

   
                         
                                                

   






                                      
const ObservableStore = require('obs-store')
const normalizeAddress = require('../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
  }

  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()
    }
    this.store.updateState({ frequentRpcList: rpcList })
    return Promise.resolve()
  }

  getFrequentRpcList () {
    return this.store.getState().frequentRpcList
  }

  //
  // PRIVATE METHODS
  //

}

module.exports = PreferencesController