blob: 3c2a73dd7829002ef0b7ca964ebdc70c1cf253a7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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
//
setAddressBook (address, name) {
return this.addToAddressBook(address, name)
.then((addressBook) => {
this.store.updateState({
addressBook,
})
return Promise.resolve()
})
}
addToAddressBook (address, name) {
let addressBook = this.getAddressBook()
let index = addressBook.findIndex((element) => { return element.address === address || element.name === name })
if (index !== -1) {
addressBook.splice(index, 1)
}
addressBook.push({
address,
name,
})
return Promise.resolve(addressBook)
}
getAddressBook () {
return this.store.getState().addressBook
}
}
module.exports = AddressBookController
|