1
- // `[new] Blaze.Var(initializer[, equalsFunc])`
2
- //
3
- // A Var is a reactive mutable variable which may be initialized with a
4
- // value or a with a reactive function. If the initializer is a reactive
5
- // function, a Deps Computation is kicked off from the constructor
6
- // that updates the reactive variable.
1
+ /**
2
+ * ## [new] Blaze.Var(initializer[, equalsFunc])
3
+ *
4
+ * A reactive mutable variable which may be initialized with a value
5
+ * or with a function to immediately autorun.
6
+ *
7
+ * * `initializer` - A function or a non-function value to use to
8
+ * initialize the Var. If a function is given, it is called in
9
+ * a `Deps.autorun` nested within the current Deps computation.
10
+ *
11
+ * * `equalsFunc` - A comparator function that takes two arguments
12
+ * and returns a boolean. The value of the Var is only considered
13
+ * to have changed (for the purpose of invalidating Computations)
14
+ * if `equalsFunc(newValue, oldValue)` is truthy. If `equalsFunc`
15
+ * is not given, `===` is used.
16
+ *
17
+ * Blaze.Var holds a single reactive value, providing `get` and `set`
18
+ * methods that obey the usual reactive contract of a Deps data
19
+ * source. (Namely, calling `get` causes the current Computation to
20
+ * depend on the value, and calling `set` invalidates any dependent
21
+ * Computations if it changes the value.)
22
+ *
23
+ * If a function is provided as an initializer, it is called to set
24
+ * the initial value of the Var, and a Computation is started that
25
+ * sets the value of the Var each time it re-runs. Because this new
26
+ * (inner) Computation is nested in the current (outer) Computation,
27
+ * when the outer Computation is invalidated, the inner Computation
28
+ * is stopped. A Var whose Computation is stopped continues to be
29
+ * reactively gettable and settable in the usual way.
30
+ *
31
+ * To avoid runaway Vars, an outer Computation is required to create a
32
+ * Var with a function as initializer. As long as the outer Computation
33
+ * is eventually invalidated or stopped, the Var will eventually
34
+ * stop recomputing its value.
35
+ *
36
+ * Example:
37
+ *
38
+ * ```
39
+ var a = Blaze.Var(1);
40
+ var b = Blaze.Var(1);
41
+
42
+ Deps.autorun(function () {
43
+ console.log('LOG:', a.get() + b.get());
44
+ });
45
+ // => LOG: 2
46
+
47
+ // These statements are assumed to be typed one at a time
48
+ // at the console, giving the autorun a chance to re-run
49
+ // between them.
50
+ b.set(2); // => LOG: 3
51
+ a.set(2); // => LOG: 4
52
+ a.set(10), b.set(10); // => LOG: 20 (printed once)
53
+ * ```
54
+ *
55
+ * To use a Var with an initializer function, an outer autorun is necessary
56
+ * and is used to stop the recomputation.
57
+ *
58
+ * Example:
59
+ *
60
+ * ```
61
+ var a = Blaze.Var(1);
62
+ var b = Blaze.Var(1);
63
+
64
+ var handle = Deps.autorun(function () {
65
+ var c = Blaze.Var(function () {
66
+ return a.get() + b.get();
67
+ });
68
+
69
+ Deps.autorun(function () {
70
+ console.log('LOG:', c.get());
71
+ });
72
+ });
73
+ // => LOG: 2
74
+
75
+ // These statements are assumed to be typed one at a time
76
+ // at the console.
77
+ b.set(2); // => LOG: 3
78
+ a.set(2); // => LOG: 4
79
+ a.set(10), b.set(10); // => LOG: 20 (printed once)
80
+ handle.stop();
81
+ a.set(1); // nothing printed
82
+ * ```
83
+ */
84
+
7
85
Blaze . Var = function ( initializer , equalsFunc ) {
8
86
var self = this ;
9
87
@@ -35,12 +113,24 @@ Blaze.Var = function (initializer, equalsFunc) {
35
113
} ;
36
114
37
115
_ . extend ( Blaze . Var . prototype , {
116
+ /**
117
+ * ## Blaze.Var#get()
118
+ *
119
+ * Returns the current value of the Var, causing the current
120
+ * Deps Computation (if any) to depend on the value.
121
+ */
38
122
get : function ( ) {
39
123
if ( Deps . active )
40
124
this . dep . depend ( ) ;
41
125
42
126
return this . curValue ;
43
127
} ,
128
+ /**
129
+ * ## Blaze.Var#set(newValue)
130
+ *
131
+ * Sets the current value of the Var, causing any dependent
132
+ * Computations to be invalidated.
133
+ */
44
134
set : function ( newValue ) {
45
135
var equals = this . equalsFunc ;
46
136
var oldValue = this . curValue ;
@@ -55,6 +145,12 @@ _.extend(Blaze.Var.prototype, {
55
145
this . curValue = newValue ;
56
146
this . dep . changed ( ) ;
57
147
} ,
148
+ /**
149
+ * ## Blaze.Var#toString()
150
+ *
151
+ * Returns a String representation of the Var, which
152
+ * includes the string form of its value.
153
+ */
58
154
toString : function ( ) {
59
155
return 'Var{' + this . get ( ) + '}' ;
60
156
}
0 commit comments