You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// // Combines both signal writes into one update. Once the callback
123
+
/// // returns the `effect` will trigger and we'll log "Foo Bar"
124
+
/// batch(() {
125
+
/// name.value = "Foo";
126
+
/// surname.value = "Bar";
127
+
/// });
128
+
/// ```
129
+
///
130
+
/// When you access a signal that you wrote to earlier inside the callback, or access a computed signal that was invalidated by another signal, we'll only update the necessary dependencies to get the current value for the signal you read from. All other invalidated signals will update at the end of the callback function.
/// // Signal is invalidated, but update is not flushed because
161
+
/// // we're still inside another batch
162
+
/// counter.value = 1;
163
+
/// });
164
+
///
165
+
/// // Still not updated...
166
+
/// });
167
+
/// // Now the callback completed and we'll trigger the effect.
168
+
/// ```
110
169
Tbatch<T>(BatchCallback<T> callback) {
111
170
if (batchDepth >0) {
112
171
returncallback();
@@ -126,6 +185,20 @@ int untrackedDepth = 0;
126
185
127
186
typedefUntrackedCallback<T> =TFunction();
128
187
188
+
/// In case when you're receiving a callback that can read some signals, but you don't want to subscribe to them, you can use `untracked` to prevent any subscriptions from happening.
189
+
///
190
+
/// ```dart
191
+
/// final counter = signal(0);
192
+
/// final effectCount = signal(0);
193
+
/// final fn = () => effectCount.value + 1;
194
+
///
195
+
/// effect(() {
196
+
/// print(counter.value);
197
+
///
198
+
/// // Whenever this effect is triggered, run `fn` that gives new value
199
+
/// effectCount.value = untracked(fn);
200
+
/// });
201
+
/// ```
129
202
Tuntracked<T>(UntrackedCallback<T> callback) {
130
203
if (untrackedDepth >0) {
131
204
returncallback();
@@ -243,7 +316,22 @@ abstract class ReadonlySignal<T> {
243
316
/// Return the value when invoked
244
317
Tcall();
245
318
246
-
/// Returns the current value without subscribing to updates.
319
+
/// In the rare instance that you have an effect that should write to another signal based on the previous value, but you _don't_ want the effect to be subscribed to that signal, you can read a signals's previous value via `signal.peek()`.
320
+
///
321
+
/// ```dart
322
+
/// final counter = signal(0);
323
+
/// final effectCount = signal(0);
324
+
///
325
+
/// effect(() {
326
+
/// print(counter.value);
327
+
///
328
+
/// // Whenever this effect is triggered, increase `effectCount`.
329
+
/// // But we don't want this signal to react to `effectCount`
330
+
/// effectCount.value = effectCount.peek() + 1;
331
+
/// });
332
+
/// ```
333
+
///
334
+
/// Note that you should only use `signal.peek()` if you really need it. Reading a signal's value via `signal.value` is the preferred way in most scenarios.
247
335
Tpeek();
248
336
249
337
/// Subscribe to value changes
@@ -406,6 +494,21 @@ class Signal<T> implements MutableSignal<T> {
406
494
Node? _targets;
407
495
}
408
496
497
+
/// The `signal` function creates a new signal. A signal is a container for a value that can change over time. You can read a signal's value or subscribe to value updates by accessing its `.value` property.
/// Writing to a signal is done by setting its `.value` property. Changing a signal's value synchronously updates every [computed](#computedfn) and [effect](#effectfn) that depends on that signal, ensuring your app state is always consistent.
@@ -695,6 +798,27 @@ class Computed<T> implements Listenable, ReadonlySignal<T> {
695
798
696
799
typedefComputedCallback<T> =TFunction();
697
800
801
+
/// Data is often derived from other pieces of existing data. The `computed` function lets you combine the values of multiple signals into a new signal that can be reacted to, or even used by additional computeds. When the signals accessed from within a computed callback change, the computed callback is re-executed and its new return value becomes the computed signal's value.
/// // Updates flow through computed, but only if someone
815
+
/// // subscribes to it. More on that later.
816
+
/// name.value = "John";
817
+
/// // Logs: "John Doe"
818
+
/// print(fullName.value);
819
+
/// ```
820
+
///
821
+
/// Any signal that is accessed inside the `computed`'s callback function will be automatically subscribed to and tracked as a dependency of the computed signal.
698
822
ReadonlySignal<T> computed<T>(
699
823
ComputedCallback<T> compute, {
700
824
String? debugLabel,
@@ -822,6 +946,43 @@ class Effect implements Listenable {
822
946
}
823
947
}
824
948
949
+
/// The `effect` function is the last piece that makes everything reactive. When you access a signal inside its callback function, that signal and every dependency of said signal will be activated and subscribed to. In that regard it is very similar to [`computed(fn)`](#computedfn). By default all updates are lazy, so nothing will update until you access a signal inside `effect`.
0 commit comments