-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
72 lines (61 loc) · 1.77 KB
/
store.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class AbstractStore {
constructor(state = {}, actions = {}) {
this._state = state
this.actions = []
Object.keys(actions).map((name) => this.addAction(name, actions[name]))
this.subscribers = []
}
addAction(name, fn) {
this.actions[name] = fn.bind(this)
}
subscribe(subscriber, props = []) {
this.subscribers.push({ target: subscriber, props: this.propsNames(props) })
}
get state() {
return this._state
}
setState(newState) {
this._state = { ...this.state, ...newState }
this.notify()
}
getSelectedState(props = []) {
return props.reduce((selectedState, prop) => {
if (prop in this.state) {
selectedState[prop] = this.state[prop]
}
return selectedState
}, {})
}
propsNames(props) {
return Array.isArray(props) ? props : Object.keys(props)
}
notify() {
this.subscribers.forEach(({ target, props }) => {
this.notifyCall(target, this.getSelectedState(props))
target.render(this.getSelectedState(props))
})
}
/**
* This method is specific to your project and should be overridden.
* @param {*} target object that has the method to be called.
* @param Array props list of state properties to send.
*/
notifyCall(target, props) {
throw new Error('notifyCall() is not implemented!')
}
dispatch(actionName, ...payload) {
const action = this._actions[actionName]
if (!action) throw new Error(`action "${actionName}" does not exist!`)
action(...payload)
}
}
class LegoStore extends AbstractStore {
subscribe(subscriber, props = []) {
super.subscribe(subscriber, props)
subscriber.setState(this.getSelectedState(this.propsNames(props)))
}
notifyCall(target, props) {
target.render(props)
}
}
export { AbstractStore, LegoStore }