Skip to content

Commit f168ea1

Browse files
authored
Update 04-$effect.md
1 parent 1d773ef commit f168ea1

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

documentation/docs/02-runes/04-$effect.md

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
23
title: $effect
34
---
45

@@ -179,7 +180,13 @@ Apart from the timing, `$effect.pre` works exactly like `$effect`.
179180

180181
## `$effect.tracking`
181182

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+
183190

184191
```svelte
185192
<script>
@@ -188,12 +195,16 @@ The `$effect.tracking` rune is an advanced feature that tells you whether or not
188195
$effect(() => {
189196
console.log('in effect:', $effect.tracking()); // true
190197
});
198+
199+
$effect.pre(() => {
200+
console.log('in effect.pre:', $effect.tracking()); // true
201+
});
191202
</script>
192203
193204
<p>in template: {$effect.tracking()}</p> <!-- true -->
194205
```
195206

196-
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:
197208

198209
```ts
199210
import { tick } from 'svelte';
@@ -209,31 +220,32 @@ export default function readable<T>(
209220

210221
return {
211222
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()) return value
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
237+
return () => {
238+
tick().then(() => {
239+
subscribers--;
240+
// If it was the last subscriber...
241+
if (subscribers === 0) {
242+
// ...stop listening to changes
243+
stop?.();
244+
stop = null;
245+
}
246+
});
247+
};
248+
});
237249

238250
return value;
239251
}

0 commit comments

Comments
 (0)