Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-candles-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: more exhaustive check during `SvelteMap.set` in deriveds
17 changes: 10 additions & 7 deletions packages/svelte/src/reactivity/map.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<typeof v_reactions>} */ (v_reactions).has(r)
);
if (needs_version_increase) {
increment(version);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { test } from '../../test';

export default test({
html: `Loading`,

async test({ assert, target }) {
await Promise.resolve();
flushSync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
</script>

{#if value instanceof Promise}
Expand Down