1 |
|
2 | var _Object = Object;
|
3 | var _Object_hasOwnProperty = _Object.hasOwnProperty;
|
4 | var _Array_isArray = Array.isArray;
|
5 | var _Error = Error;
|
6 | var _TypeError = TypeError;
|
7 |
|
8 |
|
9 | var defineProperty = _Object.defineProperty;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function hasOwnProperty(object, key) {
|
18 | return key !== "__proto__" && _Object_hasOwnProperty.call(object, key);
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var createSymbol =
|
30 | typeof Symbol === "function"
|
31 | ? Symbol
|
32 | : function (description) {
|
33 | return "__" + description;
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 | var HINT_OBSERVE = createSymbol("observe");
|
39 |
|
40 | var HINT_DISPOSE = createSymbol("dispose");
|
41 |
|
42 | var HINT_DEPENDS = createSymbol("depends");
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function defineHint(object, hint, value) {
|
51 | defineProperty(object, hint, {
|
52 | value: value,
|
53 | configurable: value !== void 0,
|
54 | enumerable: false,
|
55 | writable: false
|
56 | });
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function isObject(value) {
|
65 | return value !== null && typeof value === "object" && !_Array_isArray(value);
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function isFunction(value) {
|
74 | return typeof value === "function";
|
75 | }
|
76 |
|
77 |
|
78 | var MESSAGE_NOT_OBJECT = "Argument 'object' is not an object";
|
79 |
|
80 | var MESSAGE_NOT_FUNCTION = "Argument 'func' is not a function";
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function throwError(message, generic) {
|
88 | throw new (generic ? _Error : _TypeError)(message);
|
89 | }
|
90 |
|
91 |
|
92 | var MAX_QUEUE = 2000;
|
93 |
|
94 |
|
95 | var computedLock = false;
|
96 |
|
97 | var computedQueue = [];
|
98 |
|
99 | var computedI = 0;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | function computedOverflow() {
|
105 | var message = "Computed queue overflow! Last 10 functions in the queue:";
|
106 |
|
107 | var length = computedQueue.length;
|
108 | for (var i = length - 11; i < length; i++) {
|
109 | var func = computedQueue[i];
|
110 | message +=
|
111 | "\n"
|
112 | + (i + 1)
|
113 | + ": "
|
114 | + (func.name || "anonymous");
|
115 | }
|
116 |
|
117 | throwError(message, true);
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function computedNotify(func) {
|
125 | if (hasOwnProperty(func, HINT_DISPOSE)) return;
|
126 |
|
127 |
|
128 | if (computedQueue.lastIndexOf(func) >= computedI) return;
|
129 | computedQueue.push(func);
|
130 |
|
131 |
|
132 | if (!hasOwnProperty(func, HINT_DEPENDS)) {
|
133 | defineHint(func, HINT_DEPENDS, []);
|
134 | }
|
135 |
|
136 |
|
137 | if (!computedLock) {
|
138 | computedLock = true;
|
139 |
|
140 | try {
|
141 | for (; computedI < computedQueue.length; computedI++) {
|
142 |
|
143 | (0, computedQueue[computedI])();
|
144 | if (computedI > MAX_QUEUE) computedOverflow();
|
145 | }
|
146 | } finally {
|
147 | computedLock = false;
|
148 | computedQueue = [];
|
149 | computedI = 0;
|
150 | }
|
151 | }
|
152 | }
|
153 |
|
154 |
|
155 | function computed(func) {
|
156 | if (!isFunction(func)) {
|
157 | throwError(MESSAGE_NOT_FUNCTION);
|
158 | }
|
159 |
|
160 | computedNotify(func);
|
161 | return func;
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function reactiveProperty(value) {
|
170 | if (isObject(value)) reactiveObserve(value);
|
171 |
|
172 |
|
173 | var depends = [];
|
174 | |
175 |
|
176 |
|
177 |
|
178 | function dependsRemove(func) {
|
179 | var i = depends.lastIndexOf(func);
|
180 | if (i >= 0) depends.splice(i, 1);
|
181 | }
|
182 |
|
183 | return {
|
184 | get: function() {
|
185 |
|
186 | var func = computedQueue[computedI];
|
187 | if (func) {
|
188 | var i = depends.lastIndexOf(func);
|
189 | if (i < 0) {
|
190 |
|
191 | depends.push(func);
|
192 |
|
193 | func[HINT_DEPENDS].push(dependsRemove);
|
194 | }
|
195 | }
|
196 |
|
197 | return value;
|
198 | },
|
199 | set: function(newValue) {
|
200 | if (isObject(newValue)) reactiveObserve(newValue);
|
201 | value = newValue;
|
202 |
|
203 |
|
204 | for (var i = 0; i < depends.length; i++) {
|
205 | computedNotify(depends[i]);
|
206 | }
|
207 | }
|
208 | };
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | function reactiveObserve(object) {
|
216 | if (hasOwnProperty(object, HINT_OBSERVE)) return;
|
217 | defineHint(object, HINT_OBSERVE);
|
218 |
|
219 | for (var key in object) {
|
220 | if (hasOwnProperty(object, key)) {
|
221 | try {
|
222 | defineProperty(object, key, reactiveProperty(object[key]));
|
223 | } catch (err) {}
|
224 | }
|
225 | }
|
226 | }
|
227 |
|
228 |
|
229 | function observe(object) {
|
230 | if (!isObject(object) && !isFunction(object)) {
|
231 | throwError(MESSAGE_NOT_OBJECT);
|
232 | }
|
233 |
|
234 | reactiveObserve(object);
|
235 | return object;
|
236 | }
|
237 |
|
238 |
|
239 | function ignore(object) {
|
240 | if (!isObject(object) && !isFunction(object)) {
|
241 | throwError(MESSAGE_NOT_OBJECT);
|
242 | }
|
243 |
|
244 | if (!hasOwnProperty(object, HINT_OBSERVE)) {
|
245 | defineHint(object, HINT_OBSERVE);
|
246 | }
|
247 |
|
248 | return object;
|
249 | }
|
250 |
|
251 |
|
252 | function dispose(func, clean) {
|
253 | if (func == null) {
|
254 | func = computedQueue[computedI];
|
255 | if (!func) {
|
256 | throwError("Tried to dispose of current computed function while not running a computed function", true);
|
257 | }
|
258 | } else if (!isFunction(func)) {
|
259 | throwError(MESSAGE_NOT_FUNCTION);
|
260 | }
|
261 |
|
262 |
|
263 | if (!hasOwnProperty(func, HINT_DISPOSE)) {
|
264 |
|
265 | if (!clean) defineHint(func, HINT_DISPOSE);
|
266 |
|
267 |
|
268 | var depends = func[HINT_DEPENDS];
|
269 | if (depends) {
|
270 | defineHint(func, HINT_DEPENDS, clean ? [] : void 0);
|
271 | for (var i = 0; i < depends.length; i++) {
|
272 | depends[i](func);
|
273 | }
|
274 | }
|
275 |
|
276 |
|
277 | if (computedLock) {
|
278 | var i = computedQueue.lastIndexOf(func);
|
279 | if (i > computedI) computedQueue.splice(i, 1);
|
280 | }
|
281 | }
|
282 |
|
283 |
|
284 | if (!computedLock) return func;
|
285 | }
|
286 |
|
287 | exports.computed = computed;
|
288 | exports.dispose = dispose;
|
289 | exports.ignore = ignore;
|
290 | exports.observe = observe;
|
291 |
|