-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
54 lines (43 loc) · 1.05 KB
/
Copy pathstore.js
File metadata and controls
54 lines (43 loc) · 1.05 KB
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
/**
* Created by b1ncer on 2017/2/22.
* store: 存储状态。
* 参数:
* init @type - Function 使用 dispatch 参数初始化 store 。
* Usage: const store =
*/
const Emitter = function Emitter() {
const eventHandlers = []
this.on = function(handler) {
eventHandlers.push(handler)
}
this.emit = function(data) {
for (let i = 0, j = eventHandlers.length; i < j; i++) {
eventHandlers[i](data)
}
}
}
//init: dispatch => dispatch(initialStoredData)
const store = (init) => {
const emitter = new Emitter()
let storedData
const dispatch = data => {
storedData = data
emitter.emit(storedData)
}
function storeInstance() {}
function get() {
return storedData
}
storeInstance.on = handler => {
emitter.on(handler)
return storeInstance
}
storeInstance.dispatch = data => {
dispatch(data)
return storeInstance
}
storeInstance.get = get
init(dispatch)
return storeInstance
}
export default store