@@ -40,3 +40,122 @@ void computeCellsUpdateValueWhenDependenciesChange() {
40
40
input .currentValue = 3 ;
41
41
assertEquals (output .currentValue , 4 );
42
42
}
43
+
44
+ test
45
+ void computeCellsCanDependOnOtherComputeCells () {
46
+ value r = Reactor ();
47
+ value input = r .newInputCell (1 );
48
+ value timesTwo = r .newComputeCell1 (input , (x ) => x * 2 );
49
+ value timesThirty = r .newComputeCell1 (input , (x ) => x * 30 );
50
+ value output = r .newComputeCell2 (timesTwo , timesThirty , (x , y ) => x + y );
51
+
52
+ assertEquals (output .currentValue , 32 );
53
+ input .currentValue = 3 ;
54
+ assertEquals (output .currentValue , 96 );
55
+ }
56
+
57
+ test
58
+ void computeCellsFireCallbacks () {
59
+ value r = Reactor ();
60
+ value input = r .newInputCell (1 );
61
+ value output = r .newComputeCell1 (input , (x ) => x + 1 );
62
+
63
+ variable Element [] vals = [];
64
+ output .addCallback ((x ) => vals = vals .withTrailing (x ));
65
+
66
+ input .currentValue = 3 ;
67
+ assertEquals (vals , [4 ]);
68
+ }
69
+
70
+ test
71
+ void callbacksOnlyFireOnChange () {
72
+ value r = Reactor ();
73
+ value input = r .newInputCell (1 );
74
+ value output = r .newComputeCell1 (input , (x ) => if (x < 3 ) then 111 else 222 );
75
+
76
+ variable Element [] vals = [];
77
+ output .addCallback ((x ) => vals = vals .withTrailing (x ));
78
+
79
+ input .currentValue = 2 ;
80
+ assertEquals (vals , []);
81
+
82
+ input .currentValue = 4 ;
83
+ assertEquals (vals , [222 ]);
84
+ }
85
+
86
+ test
87
+ void callbacksCanBeAddedAndRemoved () {
88
+ value r = Reactor ();
89
+ value input = r .newInputCell (11 );
90
+ value output = r .newComputeCell1 (input , (x ) => x + 1 );
91
+
92
+ variable Element [] vals1 = [];
93
+ value sub1 = output .addCallback ((x ) => vals1 = vals1 .withTrailing (x ));
94
+ variable Element [] vals2 = [];
95
+ output .addCallback ((x ) => vals2 = vals2 .withTrailing (x ));
96
+
97
+ input .currentValue = 31 ;
98
+
99
+ sub1 .cancel ();
100
+ variable Element [] vals3 = [];
101
+ output .addCallback ((x ) => vals3 = vals3 .withTrailing (x ));
102
+
103
+ input .currentValue = 41 ;
104
+
105
+ assertEquals (vals1 , [32 ]);
106
+ assertEquals (vals2 , [32 , 42 ]);
107
+ assertEquals (vals3 , [42 ]);
108
+ }
109
+
110
+ test
111
+ void removingCallbackMultipleTimesDoesntInterfereWithOtherCallbacks () {
112
+ value r = Reactor ();
113
+ value input = r .newInputCell (1 );
114
+ value output = r .newComputeCell1 (input , (x ) => x + 1 );
115
+
116
+ variable Element [] vals1 = [];
117
+ value sub1 = output .addCallback ((x ) => vals1 = vals1 .withTrailing (x ));
118
+ variable Element [] vals2 = [];
119
+ output .addCallback ((x ) => vals2 = vals2 .withTrailing (x ));
120
+
121
+ for (i in 1 ..10 ) {
122
+ sub1 .cancel ();
123
+ }
124
+
125
+ input .currentValue = 2 ;
126
+ assertEquals (vals1 , []);
127
+ assertEquals (vals2 , [3 ]);
128
+ }
129
+
130
+ test
131
+ void callbacksAreOnlyCalledOnceEvenIfMultipleDependenciesChange () {
132
+ value r = Reactor ();
133
+ value input = r .newInputCell (1 );
134
+ value plusOne = r .newComputeCell1 (input , (x ) => x + 1 );
135
+ value minusOne1 = r .newComputeCell1 (input , (x ) => x - 1 );
136
+ value minusOne2 = r .newComputeCell1 (minusOne1 , (x ) => x - 1 );
137
+ value output = r .newComputeCell2 (plusOne , minusOne2 , (x , y ) => x * y );
138
+
139
+ variable Element [] vals = [];
140
+ output .addCallback ((x ) => vals = vals .withTrailing (x ));
141
+
142
+ input .currentValue = 4 ;
143
+ assertEquals (vals , [10 ]);
144
+ }
145
+
146
+ test
147
+ void callbacksAreNotCalledIfDependenciesChangeButOutputValueDoesntChange () {
148
+ value r = Reactor ();
149
+ value input = r .newInputCell (1 );
150
+ value plusOne = r .newComputeCell1 (input , (x ) => x + 1 );
151
+ value minusOne = r .newComputeCell1 (input , (x ) => x - 1 );
152
+ value alwaysTwo = r .newComputeCell2 (plusOne , minusOne , (x , y ) => x - y );
153
+
154
+ variable Element [] vals = [];
155
+ alwaysTwo .addCallback ((x ) => vals = vals .withTrailing (x ));
156
+
157
+ for (i in 1 ..10 ) {
158
+ input .currentValue = i ;
159
+ }
160
+ assertEquals (vals , []);
161
+ }
0 commit comments