-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (151 loc) · 3.3 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
170
171
172
var index = 1;
function uid() {
return index++;
}
var empty = undefined;
export function ignoreInitial(code) {
let init = false;
return function() {
if (init) return code.apply(this, arguments);
else init = true;
};
}
var context = null;
export function withContext(value, code) {
context = value;
code();
context = null;
}
export function filter(rule, handler) {
return function(v, o, obj, meta) {
if (rule(v, meta)) handler.apply(this, arguments);
};
}
export function link(source, target, key) {
Object.defineProperty(target, key, {
get: () => source[key],
set: value => (source[key] = value),
});
}
export function debounce(handler, delay) {
delay = delay || 200;
let timer = null;
let data = null;
return function() {
data = arguments;
if (!timer) {
timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
handler.apply(this, data);
}, delay);
}
};
}
export function createState(data, config) {
config = config || {};
const handlers = {};
const out = {};
const observe = function(mask, handler) {
const key = uid();
handlers[key] = { mask, handler };
if (mask === "*") handler(out, empty, mask);
else handler(out[mask], empty, mask);
return key;
};
const extend = function(data, sconfig){
sconfig = sconfig || config;
// normal js object
for (const key in data) {
if (data.hasOwnProperty(key)) {
const test = data[key];
if (sconfig.nested && typeof test === "object" && test) {
out[key] = createState(test, sconfig);
} else {
reactive(out, test, key, notify);
}
}
}
}
const observeEnd = function(id) {
delete handlers[id];
};
const queue = [];
let waitInQueue = false;
var batch = function(code) {
if (typeof code !== "function") {
const values = code;
code = () => {
for (const key in values) out[key] = values[key];
};
}
waitInQueue = true;
code(out);
waitInQueue = false;
while (queue.length) {
const obj = queue.shift();
notify.apply(this, obj);
}
};
const notify = function(key, old, value, meta) {
if (waitInQueue) {
queue.push([key, old, value, meta]);
return;
}
const list = Object.keys(handlers);
for (let i = 0; i < list.length; i++) {
const obj = handlers[list[i]];
if (!obj) continue;
if (obj.mask === "*" || obj.mask === key) {
obj.handler(value, old, key, meta);
}
}
};
Object.defineProperty(out, "$changes", {
value: {
attachEvent: observe,
detachEvent: observeEnd,
},
enumerable: false,
configurable: false,
});
Object.defineProperty(out, "$observe", {
value: observe,
enumerable: false,
configurable: false,
});
Object.defineProperty(out, "$batch", {
value: batch,
enumerable: false,
configurable: false,
});
Object.defineProperty(out, "$extend", {
value: extend,
enumerable: false,
configurable: false,
});
out.$extend(data, config);
return out;
}
function reactive(obj, val, key, notify) {
Object.defineProperty(obj, key, {
get: function() {
return val;
},
set: function(value) {
var changed = false;
if (val === null || value === null) {
changed = val !== value;
} else {
changed = val.valueOf() != value.valueOf();
}
if (changed) {
var old = val;
val = value;
notify(key, old, value, context);
}
},
enumerable: true,
configurable: false,
});
}