Skip to content

fix: make SvelteSet work with async mode (alternative approach)#17903

Closed
Rich-Harris wants to merge 11 commits intoeach-block-pendingfrom
async-svelte-set-alternative
Closed

fix: make SvelteSet work with async mode (alternative approach)#17903
Rich-Harris wants to merge 11 commits intoeach-block-pendingfrom
async-svelte-set-alternative

Conversation

@Rich-Harris
Copy link
Copy Markdown
Member

Alternative to #17902

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: 88293b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Contributor

@vercel vercel bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

SvelteSet's forEach, isDisjointFrom, isSubsetOf, isSupersetOf, difference, intersection, symmetricDifference, and union methods operate on an empty parent Set instead of the actual values stored in the #sources Map.

Fix on Vercel

…ifference, intersection, symmetricDifference, and union methods operate on an empty parent Set instead of the actual values stored in the #sources Map.

This commit fixes the issue reported at packages/svelte/src/reactivity/set.js:96

## Bug Explanation

The `SvelteSet` class was refactored to store values only in the `#sources` Map (where `Source<boolean>` values indicate presence/absence) instead of calling `super.add()` to store values in the parent `Set`. The constructor was changed from:

```javascript
super(value);  // Old: values stored in parent Set
```

To:

```javascript
super();  // New: parent Set is empty
if (value) {
  for (var element of value) {
    sources.set(element, this.#source(true));  // Values stored only in #sources
  }
}
```

However, the `#init()` method was not updated to reflect this change. It still uses `set_proto[method].apply(this, v)` which calls Set prototype methods on `this` (the SvelteSet instance). Since the parent Set is now always empty, these methods operate on an empty set and return incorrect results:

- `forEach` iterates over nothing
- `isDisjointFrom` always returns true (empty set is disjoint from everything)
- `isSubsetOf` always returns true (empty set is subset of everything)
- `isSupersetOf` always returns false for non-empty sets
- `difference`, `intersection`, `symmetricDifference`, `union` all produce incorrect results

## Fix Explanation

The fix materializes the current values into a temporary native `Set` before calling the prototype methods. Since `this.values()` is already implemented to correctly iterate over the `#sources` Map and yield values where the source is truthy, we use it to create the temporary Set:

```javascript
var set = new Set(this.values());
return set_proto[method].apply(set, v);  // Now operates on the materialized Set
```

This ensures that:
1. All current values from `#sources` are collected into a proper native Set
2. The Set prototype methods operate on actual data, not an empty Set
3. Reactivity is preserved since `get(this.#version)` is still called before materializing
4. The `values()` method already calls `get(source)` for each source, tracking dependencies properly

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
@Rich-Harris
Copy link
Copy Markdown
Member Author

closing in favour of #17902, which I think has better trade-offs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant