-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (134 loc) · 3.4 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
166
167
168
169
const Prop = {
STATE: Symbol(`state`),
EVENT: Symbol(`event`),
LISTENERS: Symbol(`listeners`),
ON_ALL: Symbol(`on all`),
EMIT: Symbol(`emit`),
EMITTER: Symbol(`emmiter`),
MERGE: Symbol(`merge`),
};
const Type = {
ARRAY: `Array`,
OBJECT: `Object`,
FUNCTION: `Function`,
};
const is = (arg, type) => Object.prototype.toString.call(arg).slice(8, -1) === type;
export default class State {
constructor(defaultState = {}, separator = `.`) {
this[Prop.STATE] = {
...defaultState,
};
this[Prop.LISTENERS] = {};
this.listened = [];
this.separator = separator;
}
get state() {
return this[Prop.STATE];
}
setState(newState) {
if (!is(newState, Type.OBJECT)) {
throw new TypeError(`The argument must be an object`);
}
const diffState = this[Prop.MERGE](this[Prop.STATE], newState);
this[Prop.EMITTER](diffState);
}
on(key, callback) {
const listeners = this[Prop.LISTENERS];
const value = listeners[key];
if (value) {
value.push(callback);
return;
}
listeners[key] = [callback];
}
off(key, callback) {
const listeners = this[Prop.LISTENERS];
const value = listeners[key];
if (value && callback && is(callback, Type.FUNCTION)) {
const index = value.indexOf(callback);
if (~index) {
value.splice(index, 1);
}
}
}
onAll(callback) {
this.on(Prop.ON_ALL, callback);
}
offAll(callback) {
this.off(Prop.ON_ALL, callback);
}
listen(stateEl) {
if (this.listened.includes(stateEl)) {
return;
}
stateEl.onAll((key, value) => this.setState({
[key]: value,
}));
this.listened.push(stateEl);
}
sync(stateEl) {
State.sync(this, stateEl);
}
triggerState(propsArr) {
if (!propsArr) {
this[Prop.EMITTER](this.state);
return;
}
const state = Object.keys(this.state)
.filter(key => propsArr.includes(key))
.reduce((obj, prop) => ({
...obj,
[prop]: this.state[prop],
}), {});
this[Prop.EMITTER](state);
}
[Prop.EMIT](key, value) {
const listeners = this[Prop.LISTENERS][key];
if (listeners) {
listeners.forEach((cb) => cb(value));
}
if (!key.includes(this.separator)) {
const allListener = this[Prop.LISTENERS][Prop.ON_ALL];
if (allListener) {
allListener.forEach((cb) => cb(key, value));
}
}
}
[Prop.MERGE](oldState, newState, changed = {}) {
for (let prop in newState) {
if (newState.hasOwnProperty(prop)) {
const newValue = newState[prop];
if (oldState.hasOwnProperty(prop)) {
const oldValue = oldState[prop];
if (oldValue === newValue) {
continue;
}
if (is(newValue, Type.OBJECT)) {
changed[prop] = this[Prop.MERGE](oldValue, newValue);
continue;
}
}
oldState[prop] = newValue;
changed[prop] = newValue;
}
}
return changed;
}
[Prop.EMITTER](obj, propArr = []) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
const value = obj[prop];
propArr.push(prop);
this[Prop.EMIT](propArr.join(this.separator), value);
if (is(value, Type.OBJECT)) {
this[Prop.EMITTER](value, propArr);
}
propArr = [];
}
}
}
static sync(first, second) {
first.listen(second);
second.listen(first);
}
}