fix: more exhaustive check during `SvelteMap.set` in deriveds (#13951)

* fix: more exhaustive check during `SvelteMap.set` in deriveds

* chore: use set for version reactions

* chore: cleanup
pull/13967/head
Paolo Ricciuti 5 months ago committed by GitHub
parent 5ab65e3d59
commit 67cf3871b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: more exhaustive check during `SvelteMap.set` in deriveds

@ -1,6 +1,6 @@
/** @import { Source } from '#client' */ /** @import { Source } from '#client' */
import { DEV } from 'esm-env'; 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 { get } from '../internal/client/runtime.js';
import { increment } from './utils.js'; import { increment } from './utils.js';
@ -102,12 +102,15 @@ export class SvelteMap extends Map {
increment(version); increment(version);
} else if (prev_res !== value) { } else if (prev_res !== value) {
increment(s); 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 not every reaction of s is a reaction of version we need to also include version
if ( var v_reactions = version.reactions === null ? null : new Set(version.reactions);
(s.reactions === null && version.reactions !== null) || var needs_version_increase =
(s.reactions !== null && version.reactions === null) v_reactions === null ||
) { !s.reactions?.every((r) =>
/** @type {NonNullable<typeof v_reactions>} */ (v_reactions).has(r)
);
if (needs_version_increase) {
increment(version); increment(version);
} }
} }

@ -3,7 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
html: `Loading`, html: `Loading`,
async test({ assert, target }) { async test({ assert, target }) {
await Promise.resolve(); await Promise.resolve();
flushSync(); flushSync();

@ -24,6 +24,10 @@
} }
const value = $derived(get_async(1)); const value = $derived(get_async(1));
const value2 = $derived(get_async(1));
// both values are read before the set
value;
value2;
</script> </script>
{#if value instanceof Promise} {#if value instanceof Promise}

Loading…
Cancel
Save