-
Notifications
You must be signed in to change notification settings - Fork 3
/
tracker.js
611 lines (531 loc) · 18.5 KB
/
tracker.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import Vue from 'vue';
/////////////////////////////////////////////////////
// Package docs at http://docs.meteor.com/#tracker //
/////////////////////////////////////////////////////
/**
* @namespace Tracker
* @summary The namespace for Tracker-related methods.
*/
Tracker = {};
// Allows checking that this is Vue-enabled Tracker.
Tracker._vue = true;
// http://docs.meteor.com/#tracker_active
/**
* @summary True if there is a current computation, meaning that dependencies on reactive data sources will be tracked and potentially cause the current computation to be rerun.
* @locus Client
* @type {Boolean}
*/
Object.defineProperty(Tracker, 'active', {
enumerable: true,
configurable: true,
get: function () {
return !!Vue.observer.Dep.target;
}
});
// http://docs.meteor.com/#tracker_currentcomputation
/**
* @summary The current computation, or `null` if there isn't one. The current computation is the [`Tracker.Computation`](#tracker_computation) object created by the innermost active call to `Tracker.autorun`, and it's the computation that gains dependencies when reactive data sources are accessed.
* @locus Client
* @type {Tracker.Computation}
*/
Object.defineProperty(Tracker, 'currentComputation', {
enumerable: true,
configurable: true,
get: function () {
if (!Vue.observer.Dep.target) {
return null;
}
if (!Vue.observer.Dep.target._computation) {
// Vue.observer.Dep.target._computation is set by the constructor.
new Tracker.Computation(null, null, Vue.observer.Dep.target, privateObject);
}
return Vue.observer.Dep.target._computation;
}
});
// `true` if the `_throwFirstError` option was passed in to the call
// to Tracker.flush that we are in. When set, throw rather than log the
// first error encountered while flushing.
var throwFirstError = false;
function _debugFunc() {
// We want this code to work without Meteor, and also without
// "console" (which is technically non-standard and may be missing
// on some browser we come across, like it was on IE 7).
//
// Lazy evaluation because `Meteor` does not exist right away.(??)
return (typeof Meteor !== "undefined" ? Meteor._debug :
((typeof console !== "undefined") && console.error ?
function () { console.error.apply(console, arguments); } :
function () {}));
}
function _maybeSuppressMoreLogs(messagesLength) {
// Sometimes when running tests, we intentionally suppress logs on expected
// printed errors. Since the current implementation of _throwOrLog can log
// multiple separate log messages, suppress all of them if at least one suppress
// is expected as we still want them to count as one.
if (typeof Meteor !== "undefined") {
if (Meteor._suppressed_log_expected()) {
Meteor._suppress_log(messagesLength - 1);
}
}
}
function _throwOrLog(from, e) {
if (throwFirstError) {
throwFirstError = false;
throw e;
} else {
var printArgs = ["Exception from Tracker " + from + " function:"];
if (e.stack && e.message && e.name) {
var idx = e.stack.indexOf(e.message);
if (idx < 0 || idx > e.name.length + 2) { // check for "Error: "
// message is not part of the stack
var message = e.name + ": " + e.message;
printArgs.push(message);
}
}
printArgs.push(e.stack);
_maybeSuppressMoreLogs(printArgs.length);
for (var i = 0; i < printArgs.length; i++) {
_debugFunc()(printArgs[i]);
}
}
}
// Takes a function `f`, and wraps it in a `Meteor._noYieldsAllowed`
// block if we are running on the server. On the client, returns the
// original function (since `Meteor._noYieldsAllowed` is a
// no-op). This has the benefit of not adding an unnecessary stack
// frame on the client.
function withNoYieldsAllowed(f) {
if ((typeof Meteor === 'undefined') || Meteor.isClient) {
return f;
} else {
return function () {
var args = arguments;
Meteor._noYieldsAllowed(function () {
f.apply(null, args);
});
};
}
}
// Tracker.Computation constructor is private, so we are using this object as a guard.
// External code cannot access this, and will not be able to directly construct a
// Tracker.Computation instance.
var privateObject = {};
//
// http://docs.meteor.com/#tracker_computation
/**
* @summary A Computation object represents code that is repeatedly rerun
* in response to
* reactive data changes. Computations don't have return values; they just
* perform actions, such as rerendering a template on the screen. Computations
* are created using Tracker.autorun. Use stop to prevent further rerunning of a
* computation.
* @instancename computation
*/
Tracker.Computation = class Computation {
constructor(f, onError, watcher, _private) {
if (_private !== privateObject)
throw new Error(
"Tracker.Computation constructor is private; use Tracker.autorun");
var self = this;
// http://docs.meteor.com/#computation_stopped
/**
* @summary True if this computation has been stopped.
* @locus Client
* @memberOf Tracker.Computation
* @instance
* @name stopped
*/
Object.defineProperty(self, 'stopped', {
enumerable: true,
configurable: true,
get: function () {
return !self._vueWatcher.active;
}
});
// http://docs.meteor.com/#computation_invalidated
/**
* @summary True if this computation has been invalidated (and not yet rerun), or if it has been stopped.
* @locus Client
* @memberOf Tracker.Computation
* @instance
* @name invalidated
* @type {Boolean}
*/
self.invalidated = false;
// http://docs.meteor.com/#computation_firstrun
/**
* @summary True during the initial run of the computation at the time `Tracker.autorun` is called, and false on subsequent reruns and at other times.
* @locus Client
* @memberOf Tracker.Computation
* @instance
* @name firstRun
* @type {Boolean}
*/
self._firstRun = true;
Object.defineProperty(self, 'firstRun', {
enumerable: true,
configurable: true,
get: function () {
if (self._pureWatcher) {
throw new Error("Not available for pure watchers.");
}
return self._firstRun;
}
});
Object.defineProperty(self, '_id', {
enumerable: true,
configurable: true,
get: function () {
return self._vueWatcher.id;
}
});
self._onInvalidateCallbacks = [];
self._onStopCallbacks = [];
self._onError = onError;
self._recomputing = false;
if (watcher) {
if (watcher._computation) {
// Should never happen.
throw new Error("Duplicate computation for the same pure watcher.");
}
self._vueWatcher = watcher;
watcher._computation = self;
// This computation wrapping an existing (pure) watcher.
self._pureWatcher = true;
}
else {
f = withNoYieldsAllowed(f);
var vm = (Vue.observer.Dep.target && Vue.observer.Dep.target.vm) || {_watchers: [], name: 'Tracker'};
self._vueWatcher = new Vue.observer.Watcher(vm, function (vm) {
f(self);
}, function (value, oldValue) {
// Not really used.
}, {
// We do not set deep so that callback is not really run.
deep: false,
// So that errors are not handled by the watcher.
user: false,
// We start lazy, so that it does not compute the value automatically on creation.
// We change it later on from outside.
lazy: true
});
self._vueWatcher._computation = self;
// This computation has been constructed through Tracker.autorun.
self._pureWatcher = false;
}
var originalGetter = self._vueWatcher.getter;
self._vueWatcher.getter = function () {
if (self._pureWatcher || self._firstRun) {
self.invalidated = false;
return originalGetter.apply(this, arguments);
}
else {
self._recomputing = true;
try {
if (self._needsRecompute()) {
try {
self.invalidated = false;
return originalGetter.apply(this, arguments);
} catch (e) {
if (self._onError) {
self._onError(e);
} else {
_throwOrLog("recompute", e);
}
}
}
} finally {
self._recomputing = false;
}
}
};
var originalTeardown = self._vueWatcher.teardown;
self._vueWatcher.teardown = function () {
if (!self.stopped) {
originalTeardown.call(this);
self.invalidate();
for(var i = 0, f; f = self._onStopCallbacks[i]; i++) {
Tracker.nonreactive(function () {
withNoYieldsAllowed(f)(self);
});
}
self._onStopCallbacks = [];
}
};
var originalUpdate = self._vueWatcher.update;
self._vueWatcher.update = function () {
if (!self.invalidated) {
if (self._pureWatcher || !self.stopped) {
originalUpdate.call(this);
}
self.invalidated = true;
// callbacks can't add callbacks, because
// self.invalidated === true.
for(var i = 0, f; f = self._onInvalidateCallbacks[i]; i++) {
Tracker.nonreactive(function () {
withNoYieldsAllowed(f)(self);
});
}
self._onInvalidateCallbacks = [];
}
else if (self._pureWatcher) {
originalUpdate.call(this);
}
};
if (!self._pureWatcher) {
// We started lazy to not run computation before we
// prepared everything, now we turn it off.
self._vueWatcher.lazy = false;
self._vueWatcher.dirty = false;
// Run computation for the first time.
var errored = true;
try {
self._vueWatcher.run();
errored = false;
}
finally {
self._firstRun = false;
if (errored) {
self.stop();
}
}
}
}
// http://docs.meteor.com/#computation_oninvalidate
/**
* @summary Registers `callback` to run when this computation is next invalidated, or runs it immediately if the computation is already invalidated. The callback is run exactly once and not upon future invalidations unless `onInvalidate` is called again after the computation becomes valid again.
* @locus Client
* @param {Function} callback Function to be called on invalidation. Receives one argument, the computation that was invalidated.
*/
onInvalidate(f) {
if (typeof f !== 'function')
throw new Error("onInvalidate requires a function");
if (this.invalidated) {
Tracker.nonreactive(() => {
withNoYieldsAllowed(f)(this);
});
} else {
this._onInvalidateCallbacks.push(f);
}
}
/**
* @summary Registers `callback` to run when this computation is stopped, or runs it immediately if the computation is already stopped. The callback is run after any `onInvalidate` callbacks.
* @locus Client
* @param {Function} callback Function to be called on stop. Receives one argument, the computation that was stopped.
*/
onStop(f) {
if (typeof f !== 'function')
throw new Error("onStop requires a function");
if (this.stopped) {
Tracker.nonreactive(() => {
withNoYieldsAllowed(f)(this);
});
} else {
this._onStopCallbacks.push(f);
}
}
// http://docs.meteor.com/#computation_invalidate
/**
* @summary Invalidates this computation so that it will be rerun.
* @locus Client
*/
invalidate() {
this._vueWatcher.update();
}
// http://docs.meteor.com/#computation_stop
/**
* @summary Prevents this computation from rerunning.
* @locus Client
*/
stop() {
this._vueWatcher.teardown();
}
_needsRecompute() {
return this.invalidated && !this.stopped;
}
/**
* @summary Process the reactive updates for this computation immediately
* and ensure that the computation is rerun. The computation is rerun only
* if it is invalidated.
* @locus Client
*/
flush() {
if (this._recomputing)
return;
this._vueWatcher.get(true);
}
/**
* @summary Causes the function inside this computation to run and
* synchronously process all reactive updtes.
* @locus Client
*/
run() {
this.invalidate();
this.flush();
}
};
//
// http://docs.meteor.com/#tracker_dependency
/**
* @summary A Dependency represents an atomic unit of reactive data that a
* computation might depend on. Reactive data sources such as Session or
* Minimongo internally create different Dependency objects for different
* pieces of data, each of which may be depended on by multiple computations.
* When the data changes, the computations are invalidated.
* @class
* @instanceName dependency
*/
Tracker.Dependency = class Dependency {
constructor() {
this._vueDep = new Vue.observer.Dep();
}
// http://docs.meteor.com/#dependency_depend
//
// Adds `computation` to this set if it is not already
// present. Returns true if `computation` is a new member of the set.
// If no argument, defaults to currentComputation, or does nothing
// if there is no currentComputation.
/**
* @summary Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.
If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.
Returns true if the computation is a new dependent of `dependency` rather than an existing one.
* @locus Client
* @param {Tracker.Computation} [fromComputation] An optional computation declared to depend on `dependency` instead of the current computation.
* @returns {Boolean}
*/
depend(computation) {
if (! computation) {
if (! Tracker.active)
return false;
computation = Tracker.currentComputation;
}
var existing = computation._vueWatcher.newDepIds.has(this._vueDep.id) || computation._vueWatcher.depIds.has(this._vueDep.id);
computation._vueWatcher.addDep(this._vueDep);
return existing;
}
// http://docs.meteor.com/#dependency_changed
/**
* @summary Invalidate all dependent computations immediately and remove them as dependents.
* @locus Client
*/
changed() {
this._vueDep.notify()
}
// http://docs.meteor.com/#dependency_hasdependents
/**
* @summary True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.
* @locus Client
* @returns {Boolean}
*/
hasDependents() {
return !!this._vueDep.subs.length;
}
};
// http://docs.meteor.com/#tracker_flush
/**
* @summary Process all reactive updates immediately and ensure that all invalidated computations are rerun.
* @locus Client
*/
Tracker.flush = function (options) {
throwFirstError = options && options._throwFirstError;
try {
// Tracker flush does not limit the number of updates, but watcher does.
// So we set the limit to 10000 which is infinity for most practical purposes.
Vue.observer.forceFlush((options && options._maxUpdateCount) || 10000);
}
finally {
throwFirstError = false;
}
};
/**
* @summary True if we are computing a computation now, either first time or recompute. This matches Tracker.active unless we are inside Tracker.nonreactive, which nullfies currentComputation even though an enclosing computation may still be running.
* @locus Client
* @returns {Boolean}
*/
Tracker.inFlush = function () {
return Vue.observer.isFlushing();
};
// http://docs.meteor.com/#tracker_autorun
//
// Run f(). Record its dependencies. Rerun it whenever the
// dependencies change.
//
// Returns a new Computation, which is also passed to f.
//
// Links the computation to the current computation
// so that it is stopped if the current computation is invalidated.
/**
* @callback Tracker.ComputationFunction
* @param {Tracker.Computation}
*/
/**
* @summary Run a function now and rerun it later whenever its dependencies
* change. Returns a Computation object that can be used to stop or observe the
* rerunning.
* @locus Client
* @param {Tracker.ComputationFunction} runFunc The function to run. It receives
* one argument: the Computation object that will be returned.
* @param {Object} [options]
* @param {Function} options.onError Optional. The function to run when an error
* happens in the Computation. The only argument it receives is the Error
* thrown. Defaults to the error being logged to the console.
* @returns {Tracker.Computation}
*/
Tracker.autorun = function (f, options) {
if (typeof f !== 'function')
throw new Error('Tracker.autorun requires a function argument');
options = options || {};
var c = new Tracker.Computation(f, options.onError, null, privateObject);
if (Tracker.active)
Tracker.onInvalidate(function () {
c.stop();
});
return c;
};
// http://docs.meteor.com/#tracker_nonreactive
//
// Run `f` with no current computation, returning the return value
// of `f`. Used to turn off reactivity for the duration of `f`,
// so that reactive data sources accessed by `f` will not result in any
// computations being invalidated.
/**
* @summary Run a function without tracking dependencies.
* @locus Client
* @param {Function} func A function to call immediately.
*/
Tracker.nonreactive = function (f) {
Vue.observer.pushTarget(null);
try {
return f();
}
finally {
Vue.observer.popTarget();
}
};
// http://docs.meteor.com/#tracker_oninvalidate
/**
* @summary Registers a new [`onInvalidate`](#computation_oninvalidate) callback on the current computation (which must exist), to be called immediately when the current computation is invalidated or stopped.
* @locus Client
* @param {Function} callback A callback function that will be invoked as `func(c)`, where `c` is the computation on which the callback is registered.
*/
Tracker.onInvalidate = function (f) {
if (!Tracker.active) {
throw new Error("Tracker.onInvalidate requires a currentComputation");
}
Tracker.currentComputation.onInvalidate(f);
};
// http://docs.meteor.com/#tracker_afterflush
/**
* @summary Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. The function will be run once and not on subsequent flushes unless `afterFlush` is called again.
* @locus Client
* @param {Function} callback A function to call at flush time.
*/
Tracker.afterFlush = function (f) {
Vue.observer.afterFlush(function () {
try {
f();
} catch (e) {
_throwOrLog("afterFlush", e);
}
});
};