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 <5968653+dummdidumm@users.noreply.github.com>

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/13866/head
Dominic Gannaway 11 months ago committed by GitHub
parent 86ef18165f
commit 60dcffb856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: internally wrap store subscribe in untrack

@ -1,4 +1,5 @@
/** @import { Readable } from './public' */ /** @import { Readable } from './public' */
import { untrack } from '../index-client.js';
import { noop } from '../internal/shared/utils.js'; import { noop } from '../internal/shared/utils.js';
/** /**
@ -20,10 +21,13 @@ export function subscribe_to_store(store, run, invalidate) {
} }
// Svelte store takes a private second argument // Svelte store takes a private second argument
const unsub = store.subscribe( // StartStopNotifier could mutate state, and we want to silence the corresponding validation error
run, const unsub = untrack(() =>
// @ts-expect-error store.subscribe(
invalidate run,
// @ts-expect-error
invalidate
)
); );
// Also support RxJS // Also support RxJS

@ -1,5 +1,4 @@
<script> <script>
import { untrack } from "svelte";
import { writable, derived } from "svelte/store"; import { writable, derived } from "svelte/store";
const obj = writable({ a: 1 }); const obj = writable({ a: 1 });
@ -8,9 +7,7 @@
function watch (prop) { function watch (prop) {
return derived(obj, (o) => { return derived(obj, (o) => {
untrack(() => { count++;
count++;
});
return o[prop]; return o[prop];
}); });
} }

Loading…
Cancel
Save