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
Copy file name to clipboardExpand all lines: documentation/docs/02-runes/04-$effect.md
+39-27Lines changed: 39 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
---
2
+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
2
3
title: $effect
3
4
---
4
5
@@ -179,7 +180,13 @@ Apart from the timing, `$effect.pre` works exactly like `$effect`.
179
180
180
181
## `$effect.tracking`
181
182
182
-
The `$effect.tracking` rune is an advanced feature that tells you whether or not the code is running inside a tracking context, such as an effect or inside your template ([demo](/playground/untitled#H4sIAAAAAAAACn3PwYrCMBDG8VeZDYIt2PYeY8Dn2HrIhqkU08nQjItS-u6buAt7UDzmz8ePyaKGMWBS-nNRcmdU-hHUTpGbyuvI3KZvDFLal0v4qvtIgiSZUSb5eWSxPfWSc4oB2xDP1XYk8HHiSHkICeXKeruDDQ4Demlldv4y0rmq6z10HQwuJMxGVv4mVVXDwcJS0jP9u3knynwtoKz1vifT_Z9Jhm0WBCcOTlDD8kyspmML5qNpHg40jc3fFryJ0iWsp_UHgz3180oBAAA=)):
183
+
```js
184
+
// @noErrors
185
+
const$effect.tracking:boolean;
186
+
```
187
+
188
+
The `$effect.tracking` rune is `true` when used inside a reactive context, such as an `$effect`, `$effect.pre` or in your svelte markup ([demo](/playground/untitled#H4sIAAAAAAAACn3PwYrCMBDG8VeZDYIt2PYeY8Dn2HrIhqkU08nQjItS-u6buAt7UDzmz8ePyaKGMWBS-nNRcmdU-hHUTpGbyuvI3KZvDFLal0v4qvtIgiSZUSb5eWSxPfWSc4oB2xDP1XYk8HHiSHkICeXKeruDDQ4Demlldv4y0rmq6z10HQwuJMxGVv4mVVXDwcJS0jP9u3knynwtoKz1vifT_Z9Jhm0WBCcOTlDD8kyspmML5qNpHg40jc3fFryJ0iWsp_UHgz3180oBAAA=)):
189
+
183
190
184
191
```svelte
185
192
<script>
@@ -188,12 +195,16 @@ The `$effect.tracking` rune is an advanced feature that tells you whether or not
This allows you to (for example) add things like subscriptions without causing memory leaks, by putting them in child effects. Here's a `readable` function that listens to changes from a callback function as long as it's inside a tracking context:
207
+
This allows you to (for example) add things like subscriptions without causing memory leaks, by putting them in child effects. Here's a `readable` function that listens to changes from a callback function as long as it's inside a reactive context:
197
208
198
209
```ts
199
210
import { tick } from'svelte';
@@ -209,31 +220,32 @@ export default function readable<T>(
209
220
210
221
return {
211
222
get value() {
212
-
// If in a tracking context ...
213
-
if ($effect.tracking()) {
214
-
$effect(() => {
215
-
// ...and there's no subscribers yet...
216
-
if (subscribers===0) {
217
-
// ...invoke the function and listen to changes to update state
218
-
stop=start((fn) => (value=fn(value)));
219
-
}
220
-
221
-
subscribers++;
222
-
223
-
// The return callback is called once a listener unlistens
224
-
return () => {
225
-
tick().then(() => {
226
-
subscribers--;
227
-
// If it was the last subscriber...
228
-
if (subscribers===0) {
229
-
// ...stop listening to changes
230
-
stop?.();
231
-
stop=null;
232
-
}
233
-
});
234
-
};
235
-
});
236
-
}
223
+
// If not in a reactive context
224
+
if (!$effect.tracking()) returnvalue
225
+
226
+
// If in a reactive context ...
227
+
$effect(() => {
228
+
// ...and there's no subscribers yet...
229
+
if (subscribers===0) {
230
+
// ...invoke the function and listen to changes to update state
231
+
stop=start((fn) => (value=fn(value)));
232
+
}
233
+
234
+
subscribers++;
235
+
236
+
// The return callback is called once a listener unlistens
0 commit comments