Skip to content

Commit ac982ee

Browse files
committed
fix: internally wrap store subscribe in untrack
1 parent 185e112 commit ac982ee

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

.changeset/little-hotels-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: internally wrap store subscribe in untrack

packages/svelte/src/store/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { Readable } from './public' */
2+
import { untrack } from '../index-client.js';
23
import { noop } from '../internal/shared/utils.js';
34

45
/**
@@ -20,11 +21,11 @@ export function subscribe_to_store(store, run, invalidate) {
2021
}
2122

2223
// Svelte store takes a private second argument
23-
const unsub = store.subscribe(
24+
const unsub = untrack(() => store.subscribe(
2425
run,
2526
// @ts-expect-error
2627
invalidate
27-
);
28+
));
2829

2930
// Also support RxJS
3031
// @ts-expect-error TODO fix this in the types?

packages/svelte/tests/runtime-runes/samples/store-unsubscribe-not-referenced-after/main.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script>
2-
import { untrack } from "svelte";
32
import { writable, derived } from "svelte/store";
43
54
const obj = writable({ a: 1 });
@@ -8,9 +7,7 @@
87
98
function watch (prop) {
109
return derived(obj, (o) => {
11-
untrack(() => {
12-
count++;
13-
});
10+
count++;
1411
return o[prop];
1512
});
1613
}

0 commit comments

Comments
 (0)