You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
967 B
46 lines
967 B
const ObservableStore = require('obs-store')
|
|
const extend = require('xtend')
|
|
|
|
class AddressBookController {
|
|
|
|
constructor (opts = {}) {
|
|
const initState = extend({
|
|
addressBook: [],
|
|
}, opts.initState)
|
|
this.store = new ObservableStore(initState)
|
|
}
|
|
|
|
//
|
|
// PUBLIC METHODS
|
|
//
|
|
|
|
setAddressList (address, name) {
|
|
return this.addToAddressList(address, name)
|
|
.then((addressBook) => {
|
|
this.store.updateState({
|
|
addressBook,
|
|
})
|
|
return Promise.resolve()
|
|
})
|
|
}
|
|
|
|
addToAddressList (address, name) {
|
|
let addressBook = this.getAddressList()
|
|
let index = addressBook.findIndex((element) => { return element.address === address })
|
|
if (index !== -1) {
|
|
addressBook.splice(index, 1)
|
|
}
|
|
addressBook.push({
|
|
address,
|
|
name,
|
|
})
|
|
return Promise.resolve(addressBook)
|
|
}
|
|
|
|
getAddressList () {
|
|
return this.store.getState().addressBook
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = AddressBookController
|
|
|