-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcomputed.dart
More file actions
244 lines (205 loc) · 6.09 KB
/
Copy pathcomputed.dart
File metadata and controls
244 lines (205 loc) · 6.09 KB
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
part of 'core.dart';
/// {@template computed}
/// A special Signal that notifies only whenever the selected
/// values change.
///
/// You may want to subscribe only to sub-field of a `Signal` value or to
/// combine multiple signal values.
/// ```dart
/// // first name signal
/// final firstName = Signal('Josh');
///
/// // last name signal
/// final lastName = Signal('Brown');
///
/// // derived signal, updates automatically when firstName or lastName change
/// final fullName = Computed(() => '${firstName()} ${lastName()}');
///
/// print(fullName()); // prints Josh Brown
///
/// // just update the name, the effect above doesn't run because the age has not changed
/// user.update((value) => value.copyWith(name: 'new-name'));
///
/// // just update the age, the effect above prints
/// user.update((value) => value.copyWith(age: 21));
/// ```
///
/// A derived signal is not of type `Signal` but is a `ReadSignal`.
/// The difference with a normal `Signal` is that a `ReadSignal` doesn't have a
/// value setter, in other words it's a __read-only__ signal.
///
/// You can also use derived signals in other ways, like here:
/// ```dart
/// final counter = Signal(0);
/// final doubleCounter = Computed(() => counter() * 2);
/// ```
///
/// Every time the `counter` signal changes, the doubleCounter updates with the
/// new doubled `counter` value.
///
/// You can also transform the value type like:
/// ```dart
/// final counter = Signal(0); // counter contains the value type `int`
/// final isGreaterThan5 = Computed(() => counter() > 5); // isGreaterThan5 contains the value type `bool`
/// ```
///
/// `isGreaterThan5` will update only when the `counter` value becomes lower/greater than `5`.
/// - If the `counter` value is `0`, `isGreaterThan5` is equal to `false`.
/// - If you update the value to `1`, `isGreaterThan5` doesn't emit a new
/// value, but still contains `false`.
/// - If you update the value to `6`, `isGreaterThan5` emits a new `true` value.
/// {@endtemplate}
class Computed<T> extends ReadSignal<T> {
/// {@macro computed}
Computed(
this.selector, {
/// {@macro SignalBase.name}
String? name,
/// {@macro SignalBase.equals}
super.equals,
/// {@macro SignalBase.autoDispose}
super.autoDispose,
/// {@macro SignalBase.trackInDevTools}
super.trackInDevTools,
/// {@macro SignalBase.comparator}
super.comparator = identical,
/// {@macro SignalBase.trackPreviousValue}
super.trackPreviousValue,
}) : super(name: name ?? ReactiveName.nameFor('Computed')) {
var runnedOnce = false;
_internalComputed = _AlienComputed(
this,
(previousValue) {
if (trackPreviousValue && previousValue is T) {
_hasPreviousValue = true;
_untrackedPreviousValue = _previousValue = previousValue;
}
try {
_untrackedValue = selector();
if (runnedOnce) {
_notifySignalUpdate();
} else {
runnedOnce = true;
}
return _untrackedValue;
} catch (e, s) {
throw SolidartCaughtException(e, stackTrace: s);
}
},
);
_notifySignalCreation();
}
/// The selector applied
final T Function() selector;
late final _AlienComputed<T> _internalComputed;
bool _disposed = false;
late T _untrackedValue;
T? _previousValue;
T? _untrackedPreviousValue;
// Whether or not there is a previous value
bool _hasPreviousValue = false;
// Keeps track of all the callbacks passed to [onDispose].
// Used later to fire each callback when this signal is disposed.
final _onDisposeCallbacks = <VoidCallback>[];
// A computed signal is always initialized
@override
bool get hasValue => true;
final _deps = <alien.ReactiveNode>{};
@override
void dispose() {
if (_disposed) return;
_internalComputed.dispose();
_disposed = true;
for (final dep in _deps) {
if (dep is _AlienSignal) dep.parent._mayDispose();
if (dep is _AlienComputed) dep.parent._mayDispose();
}
_deps.clear();
for (final cb in _onDisposeCallbacks) {
cb();
}
_onDisposeCallbacks.clear();
_notifySignalDisposal();
}
@override
T get value {
if (_disposed) {
return _untrackedValue;
}
final value = reactiveSystem.getComputedValue(_internalComputed);
if (autoDispose) {
Future.microtask(_mayDispose);
}
return value;
}
@override
T call() {
return value;
}
/// Returns the previous value of the computed.
@override
T? get previousValue {
if (!trackPreviousValue) return null;
// cause observation
if (!disposed) value;
return _previousValue;
}
/// Returns the untracked value of the computed.
@override
T get untrackedValue {
return _untrackedValue;
}
/// Returns the untracked previous value of the computed.
@override
T? get untrackedPreviousValue {
return _untrackedPreviousValue;
}
@override
void _mayDispose() {
if (_disposed) return;
if (_internalComputed.deps == null && _internalComputed.subs == null) {
dispose();
} else {
_deps.clear();
var link = _internalComputed.deps;
for (; link != null; link = link.nextDep) {
final dep = link.dep;
_deps.add(dep);
}
}
}
@override
bool get disposed => _disposed;
@override
bool get hasPreviousValue {
if (!trackPreviousValue) return false;
// cause observation
value;
return _hasPreviousValue;
}
// coverage:ignore-start
@override
int get listenerCount => _deps.length;
// coverage:ignore-end
@override
void onDispose(VoidCallback cb) {
_onDisposeCallbacks.add(cb);
}
// coverage:ignore-start
/// Indicates if the [oldValue] and the [newValue] are equal
@override
bool _compare(T? oldValue, T? newValue) {
// skip if the value are equals
if (equals) {
return oldValue == newValue;
}
// return the [comparator] result
return comparator(oldValue, newValue);
}
// coverage:ignore-end
@override
String toString() {
value;
return '''Computed<$T>(value: $untrackedValue, previousValue: $untrackedPreviousValue)''';
}
}