diff --git a/.changeset/sweet-candles-poke.md b/.changeset/sweet-candles-poke.md new file mode 100644 index 0000000000..49ec305399 --- /dev/null +++ b/.changeset/sweet-candles-poke.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: more exhaustive check during `SvelteMap.set` in deriveds diff --git a/packages/svelte/src/reactivity/map.js b/packages/svelte/src/reactivity/map.js index ddadad90e2..ae1bf47773 100644 --- a/packages/svelte/src/reactivity/map.js +++ b/packages/svelte/src/reactivity/map.js @@ -1,6 +1,6 @@ /** @import { Source } from '#client' */ import { DEV } from 'esm-env'; -import { source, set } from '../internal/client/reactivity/sources.js'; +import { set, source } from '../internal/client/reactivity/sources.js'; import { get } from '../internal/client/runtime.js'; import { increment } from './utils.js'; @@ -102,12 +102,15 @@ export class SvelteMap extends Map { increment(version); } else if (prev_res !== value) { increment(s); - // If no one listening to this property and is listening to the version, or - // the inverse, then we should increment the version to be safe - if ( - (s.reactions === null && version.reactions !== null) || - (s.reactions !== null && version.reactions === null) - ) { + + // if not every reaction of s is a reaction of version we need to also include version + var v_reactions = version.reactions === null ? null : new Set(version.reactions); + var needs_version_increase = + v_reactions === null || + !s.reactions?.every((r) => + /** @type {NonNullable} */ (v_reactions).has(r) + ); + if (needs_version_increase) { increment(version); } } diff --git a/packages/svelte/tests/runtime-runes/samples/derived-map/_config.js b/packages/svelte/tests/runtime-runes/samples/derived-map/_config.js index f3d115c62b..f933d0b2fa 100644 --- a/packages/svelte/tests/runtime-runes/samples/derived-map/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/derived-map/_config.js @@ -3,7 +3,6 @@ import { test } from '../../test'; export default test({ html: `Loading`, - async test({ assert, target }) { await Promise.resolve(); flushSync(); diff --git a/packages/svelte/tests/runtime-runes/samples/derived-map/main.svelte b/packages/svelte/tests/runtime-runes/samples/derived-map/main.svelte index 6278af8158..ea51f29dfb 100644 --- a/packages/svelte/tests/runtime-runes/samples/derived-map/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/derived-map/main.svelte @@ -24,6 +24,10 @@ } const value = $derived(get_async(1)); + const value2 = $derived(get_async(1)); + // both values are read before the set + value; + value2; {#if value instanceof Promise}