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
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.
60
55
56
+
You can also pass options to `signal()` and `computed()` to be notified when the signal gains its first subscriber and loses its last subscriber:
57
+
58
+
```js
59
+
constcounter=signal(0, {
60
+
watched:function () {
61
+
console.log("Signal has its first subscriber");
62
+
},
63
+
unwatched:function () {
64
+
console.log("Signal lost its last subscriber");
65
+
},
66
+
});
67
+
```
68
+
69
+
These callbacks are useful for managing resources or side effects that should only be active when the signal has subscribers. For example, you might use them to start/stop expensive background operations or subscribe/unsubscribe from external event sources.
70
+
61
71
#### `signal.peek()`
62
72
63
73
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()`.
0 commit comments