1 |
|
2 |
|
3 |
|
4 |
|
5 | var Deps =
|
6 | module.exports = {};
|
7 |
|
8 |
|
9 | Deps.active = false;
|
10 |
|
11 |
|
12 | Deps.currentComputation = null;
|
13 |
|
14 | var setCurrentComputation = function (c) {
|
15 | Deps.currentComputation = c;
|
16 | Deps.active = !! c;
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var _hasOwnProperty = Object.prototype.hasOwnProperty;
|
23 | var _assign = function (tgt, src) {
|
24 | for (var k in src) {
|
25 | if (_hasOwnProperty.call(src, k))
|
26 | tgt[k] = src[k];
|
27 | }
|
28 | return tgt;
|
29 | };
|
30 |
|
31 | var _debugFunc = function () {
|
32 |
|
33 | return (typeof Meteor !== "undefined" ? Meteor._debug :
|
34 | ((typeof console !== "undefined") && console.log ?
|
35 | function () { console.log.apply(console, arguments); } :
|
36 | function () {}));
|
37 | };
|
38 |
|
39 | var _throwOrLog = function (from, e) {
|
40 | if (throwFirstError) {
|
41 | throw e;
|
42 | } else {
|
43 | _debugFunc()("Exception from Deps " + from + " function:",
|
44 | e.stack || e.message);
|
45 | }
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | var callWithNoYieldsAllowed = function (f, comp) {
|
53 | if ((typeof Meteor === 'undefined') || Meteor.isClient) {
|
54 | f(comp);
|
55 | } else {
|
56 | Meteor._noYieldsAllowed(function () {
|
57 | f(comp);
|
58 | });
|
59 | }
|
60 | };
|
61 |
|
62 | var nextId = 1;
|
63 |
|
64 | var pendingComputations = [];
|
65 |
|
66 | var willFlush = false;
|
67 |
|
68 | var inFlush = false;
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | var inCompute = false;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | var throwFirstError = false;
|
80 |
|
81 | var afterFlushCallbacks = [];
|
82 |
|
83 | var requireFlush = function () {
|
84 | if (! willFlush) {
|
85 | requestAnimationFrame(Deps.flush);
|
86 | willFlush = true;
|
87 | }
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 | var constructingComputation = false;
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Deps.Computation = function (f, parent) {
|
98 | if (! constructingComputation)
|
99 | throw new Error(
|
100 | "Deps.Computation constructor is private; use Deps.autorun");
|
101 | constructingComputation = false;
|
102 |
|
103 | var self = this;
|
104 |
|
105 |
|
106 | self.stopped = false;
|
107 |
|
108 |
|
109 | self.invalidated = false;
|
110 |
|
111 |
|
112 | self.firstRun = true;
|
113 |
|
114 | self._id = nextId++;
|
115 | self._onInvalidateCallbacks = [];
|
116 |
|
117 |
|
118 | self._parent = parent;
|
119 | self._func = f;
|
120 | self._recomputing = false;
|
121 |
|
122 | var errored = true;
|
123 | try {
|
124 | self._compute();
|
125 | errored = false;
|
126 | } finally {
|
127 | self.firstRun = false;
|
128 | if (errored)
|
129 | self.stop();
|
130 | }
|
131 | };
|
132 |
|
133 | _assign(Deps.Computation.prototype, {
|
134 |
|
135 |
|
136 | onInvalidate: function (f) {
|
137 | var self = this;
|
138 |
|
139 | if (typeof f !== 'function')
|
140 | throw new Error("onInvalidate requires a function");
|
141 |
|
142 | if (self.invalidated) {
|
143 | Deps.nonreactive(function () {
|
144 | callWithNoYieldsAllowed(f, self);
|
145 | });
|
146 | } else {
|
147 | self._onInvalidateCallbacks.push(f);
|
148 | }
|
149 | },
|
150 |
|
151 |
|
152 | invalidate: function () {
|
153 | var self = this;
|
154 | if (! self.invalidated) {
|
155 |
|
156 |
|
157 | if (! self._recomputing && ! self.stopped) {
|
158 | requireFlush();
|
159 | pendingComputations.push(this);
|
160 | }
|
161 |
|
162 | self.invalidated = true;
|
163 |
|
164 |
|
165 |
|
166 | for(var i = 0, f; f = self._onInvalidateCallbacks[i]; i++) {
|
167 | Deps.nonreactive(function () {
|
168 | callWithNoYieldsAllowed(f, self);
|
169 | });
|
170 | }
|
171 | self._onInvalidateCallbacks = [];
|
172 | }
|
173 | },
|
174 |
|
175 |
|
176 | stop: function () {
|
177 | if (! this.stopped) {
|
178 | this.stopped = true;
|
179 | this.invalidate();
|
180 | }
|
181 | },
|
182 |
|
183 | _compute: function () {
|
184 | var self = this;
|
185 | self.invalidated = false;
|
186 |
|
187 | var previous = Deps.currentComputation;
|
188 | setCurrentComputation(self);
|
189 | var previousInCompute = inCompute;
|
190 | inCompute = true;
|
191 | try {
|
192 | callWithNoYieldsAllowed(self._func, self);
|
193 | } finally {
|
194 | setCurrentComputation(previous);
|
195 | inCompute = false;
|
196 | }
|
197 | },
|
198 |
|
199 | _recompute: function () {
|
200 | var self = this;
|
201 |
|
202 | self._recomputing = true;
|
203 | try {
|
204 | while (self.invalidated && ! self.stopped) {
|
205 | try {
|
206 | self._compute();
|
207 | } catch (e) {
|
208 | _throwOrLog("recompute", e);
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | }
|
217 | } finally {
|
218 | self._recomputing = false;
|
219 | }
|
220 | }
|
221 | });
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | Deps.Dependency = function () {
|
227 | this._dependentsById = {};
|
228 | };
|
229 |
|
230 | _assign(Deps.Dependency.prototype, {
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | depend: function (computation) {
|
238 | if (! computation) {
|
239 | if (! Deps.active)
|
240 | return false;
|
241 |
|
242 | computation = Deps.currentComputation;
|
243 | }
|
244 | var self = this;
|
245 | var id = computation._id;
|
246 | if (! (id in self._dependentsById)) {
|
247 | self._dependentsById[id] = computation;
|
248 | computation.onInvalidate(function () {
|
249 | delete self._dependentsById[id];
|
250 | });
|
251 | return true;
|
252 | }
|
253 | return false;
|
254 | },
|
255 |
|
256 |
|
257 | changed: function () {
|
258 | var self = this;
|
259 | for (var id in self._dependentsById)
|
260 | self._dependentsById[id].invalidate();
|
261 | },
|
262 |
|
263 |
|
264 | hasDependents: function () {
|
265 | var self = this;
|
266 | for(var id in self._dependentsById)
|
267 | return true;
|
268 | return false;
|
269 | }
|
270 | });
|
271 |
|
272 | _assign(Deps, {
|
273 |
|
274 | flush: function (_opts) {
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | if (inFlush)
|
287 | throw new Error("Can't call Deps.flush while flushing");
|
288 |
|
289 | if (inCompute)
|
290 | throw new Error("Can't flush inside Deps.autorun");
|
291 |
|
292 | inFlush = true;
|
293 | willFlush = true;
|
294 | throwFirstError = !! (_opts && _opts._throwFirstError);
|
295 |
|
296 | var finishedTry = false;
|
297 | try {
|
298 | while (pendingComputations.length ||
|
299 | afterFlushCallbacks.length) {
|
300 |
|
301 |
|
302 | while (pendingComputations.length) {
|
303 | var comp = pendingComputations.shift();
|
304 | comp._recompute();
|
305 | }
|
306 |
|
307 | if (afterFlushCallbacks.length) {
|
308 |
|
309 |
|
310 | var func = afterFlushCallbacks.shift();
|
311 | try {
|
312 | func();
|
313 | } catch (e) {
|
314 | _throwOrLog("afterFlush function", e);
|
315 | }
|
316 | }
|
317 | }
|
318 | finishedTry = true;
|
319 | } finally {
|
320 | if (! finishedTry) {
|
321 |
|
322 | inFlush = false;
|
323 | Deps.flush({_throwFirstError: false});
|
324 | }
|
325 | willFlush = false;
|
326 | inFlush = false;
|
327 | }
|
328 | },
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | autorun: function (f) {
|
340 | if (typeof f !== 'function')
|
341 | throw new Error('Deps.autorun requires a function argument');
|
342 |
|
343 | constructingComputation = true;
|
344 | var c = new Deps.Computation(f, Deps.currentComputation);
|
345 |
|
346 | if (Deps.active)
|
347 | Deps.onInvalidate(function () {
|
348 | c.stop();
|
349 | });
|
350 |
|
351 | return c;
|
352 | },
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 | nonreactive: function (f) {
|
361 | var previous = Deps.currentComputation;
|
362 | setCurrentComputation(null);
|
363 | try {
|
364 | return f();
|
365 | } finally {
|
366 | setCurrentComputation(previous);
|
367 | }
|
368 | },
|
369 |
|
370 |
|
371 | onInvalidate: function (f) {
|
372 | if (! Deps.active)
|
373 | throw new Error("Deps.onInvalidate requires a currentComputation");
|
374 |
|
375 | Deps.currentComputation.onInvalidate(f);
|
376 | },
|
377 |
|
378 |
|
379 | afterFlush: function (f) {
|
380 | afterFlushCallbacks.push(f);
|
381 | requireFlush();
|
382 | }
|
383 | }); |
\ | No newline at end of file |