Skip to content

Commit 60dcffb

Browse files
trueadmdummdidumm
andauthored
fix: internally wrap store subscribe in untrack (#13858)
* fix: internally wrap store subscribe in untrack * lint * Update packages/svelte/src/store/utils.js Co-authored-by: Simon H <[email protected]> --------- Co-authored-by: Simon H <[email protected]>
1 parent 86ef181 commit 60dcffb

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
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: 8 additions & 4 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,10 +21,13 @@ export function subscribe_to_store(store, run, invalidate) {
2021
}
2122

2223
// Svelte store takes a private second argument
23-
const unsub = store.subscribe(
24-
run,
25-
// @ts-expect-error
26-
invalidate
24+
// StartStopNotifier could mutate state, and we want to silence the corresponding validation error
25+
const unsub = untrack(() =>
26+
store.subscribe(
27+
run,
28+
// @ts-expect-error
29+
invalidate
30+
)
2731
);
2832

2933
// Also support RxJS

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)