-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·165 lines (153 loc) · 3.65 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* vanix v0.0.7
* (c) 2019 Syuji Higa
* @license MIT
*/
class Vanix {
/**
* @param {Object} object
* @param {Object} [object.state]
* @param {Object} [object.mutations]
* @param {Object} [object.actions]
*/
constructor({ state, mutations, actions }) {
this._update = new Map();
this._stateReadOnly = {};
this._state = {};
this._mutations = {};
this._actions = {};
this._stateReadOnly = this._createSstateReadOnly(state);
this._state = this._createState(state);
Object.assign(this._mutations, mutations || {});
Object.assign(this._actions, actions || {});
}
/**
* @return {Object}
* @property {Object} state
* @property {function} commit
* @property {function} dispatch
* @property {function} observe
* @property {function} unobserve
*/
create() {
return {
state: this._stateReadOnly,
commit: this._commit.bind(this),
dispatch: this._dispatch.bind(this),
observe: this._observe.bind(this),
unobserve: this._unobserve.bind(this)
};
}
/**
* @return {Instance}
*/
destroy() {
this._update.clear();
for (const prop of Object.keys(this._stateReadOnly)) {
delete this._stateReadOnly[prop];
}
for (const prop of Object.keys(this._state)) {
delete this._state[prop];
}
return this;
}
/**
* @param {Object} state
* @return {Object}
*/
_createSstateReadOnly(state) {
return Object.keys(state).reduce((memo, prop) => {
Object.defineProperty(memo, prop, {
enumerable: true,
configurable: true,
get: function() {
return state[prop];
}
});
return memo;
}, {});
}
/**
* @param {Object} state
* @return {Object}
*/
_createState(state) {
return Object.keys(state).reduce((memo, prop) => {
const _this = this;
Object.defineProperty(memo, prop, {
enumerable: true,
configurable: true,
get: function() {
return state[prop];
},
set: function(val) {
if (state[prop] === val) {
return;
}
state[prop] = val;
if (_this._update.has(prop)) {
_this._update.get(prop).forEach(func => {
func(_this._stateReadOnly);
});
}
}
});
return memo;
}, {});
}
/**
* @param {string} prop
* @param {*} arg
*/
_commit(prop, ...arg) {
if (!(prop in this._mutations)) {
throw new Error(`Not found "${prop}" mutations.`);
}
this._mutations[prop](this._state, ...arg);
}
/**
* @param {string} prop
* @param {*} arg
*/
_dispatch(prop, ...arg) {
if (!(prop in this._actions)) {
throw new Error(`Not found "${prop}" actions.`);
}
this._actions[prop](
{
commit: this._commit.bind(this),
state: this._state,
getters: this._stateReadOnly,
dispatch: this._dispatch.bind(this)
},
...arg
);
}
/**
* @param {Object} observeObj
* @return {Object}
*/
_observe(observeObj) {
for (const [prop, func] of Object.entries(observeObj)) {
if (!this._update.has(prop)) {
this._update.set(prop, new Set());
}
this._update.get(prop).add(func);
}
return observeObj;
}
/**
* @param {Object} observeObj
*/
_unobserve(observeObj) {
for (const [prop, func] of Object.entries(observeObj)) {
if (this._update.has(prop) && this._update.get(prop).has(func)) {
this._update.get(prop).delete(func);
if (0 >= this._update.get(prop).size) {
this._update.delete(prop);
}
}
}
}
}
export { Vanix as default };