-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (91 loc) · 3.32 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const createStore = function(actions) {
let store = {
state: null,
previousState: null,
processor: createProcessor(actions),
listeners: [],
dispatch: function(action, params) {
console.log('Action: ' + (action ? (action.prototype.name+(action.prototype.simple ? '' : '(Complex)' )) : 'Store Initialization'))
if (action === null) {
this.previousState = this.state
this.state = this.processor.process(null, action, params)
this.listeners.forEach((listener) => listener())
this.previousState = null
}
else if (action.prototype.simple===true) {
this.previousState = this.state
this.state = this.processor.process(this.state, action, params)
this.listeners.forEach((listener) => listener(this.state, this.previousState))
this.previousState = null
} else if (action.prototype.simple===false) {
action()(this, params)
}
},
subscribe: function(listener) {
this.listeners.push(listener)
return () => { this.listeners = this.listeners.filter((l) => l!==listener) }
}
}
store.dispatch(null)
return store
}
const createProcessor = function(obj, paths=[]) {
if (obj.type=='actionGroup') { // if obj is actionGroup, create a processor out of it
Object.keys(obj).forEach(function(actionName) {
let action = obj[actionName]
action.prototype.name = (paths.length==0 ? actionName : paths.join('.')+'.'+actionName)
action.prototype.simple = (typeof action({}, {}) === 'function') ? false : true
})
return {
process: function(state, action, params) {
if (state === null) {
return this.actionGroup.default()
} else {
for (let i=0; i<Object.keys(this.actionGroup).length; i++) {
let k = Object.keys(this.actionGroup)[i]
if (this.actionGroup[k]===action) {
return this.actionGroup[k](state, params)
}
}
return state
}
},
actionGroup: obj
}
} else { // if obj is a normal object, combine processors in it
let subProcessors = {}
Object.keys(obj).forEach((k) => {
subProcessors[k] = createProcessor(obj[k], [...paths, k])
})
return {
process: function(state, action, params) {
if (state === null) {
let newState = {}
Object.keys(this.subProcessors).forEach((k) => {
newState[k] = this.subProcessors[k].process(null)
})
return newState
} else {
let newState = {}
Object.keys(this.subProcessors).forEach((k) => {
newState[k] = this.subProcessors[k].process(state[k], action, params)
})
return newState
}
},
subProcessors: subProcessors
}
}
}
const createActionGroup = function(obj) {
let propertiesObject = {}
Object.keys(obj).forEach(function(actionName) {
let action = obj[actionName]
propertiesObject[actionName] = {enumerable: true, value: action}
})
propertiesObject.type = {enumerable: false, value: 'actionGroup'}
return Object.create({}, propertiesObject)
}
module.exports.createStore = createStore;
module.exports.createProcessor = createProcessor;
module.exports.createActionGroup = createActionGroup;