Skip to content

fix: make SvelteSet work with async mode#17902

Open
Rich-Harris wants to merge 40 commits intomainfrom
async-svelte-set
Open

fix: make SvelteSet work with async mode#17902
Rich-Harris wants to merge 40 commits intomainfrom
async-svelte-set

Conversation

@Rich-Harris
Copy link
Copy Markdown
Member

Follow-up to #17162. It turns out SvelteSet didn't work with async mode, because we were mutating the underlying set. To be async-safe, we need to clone it when adding or deleting keys. This does of course add overhead, but I think it's unavoidable (and likely negligible in most cases).

I have an alternative PR with a slightly different approach that I'll open in a moment. Both currently have failing tests. Assuming we can get at least one of them to work correctly, this should provide a blueprint for doing the same thing with SvelteMap.

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: d7d7c31

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

@github-actions
Copy link
Copy Markdown
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17902

vercel bot and others added 5 commits March 11, 2026 13:03
…sSupersetOf) and set_like_methods (difference, intersection, symmetricDifference, union) operate on an empty underlying Set instead of the actual data stored in #items.

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

**Bug Explanation:**

After the refactoring to make SvelteSet async-safe, the data storage changed:
- **Before:** Data was stored in the underlying Set via `super.add()` in the constructor
- **After:** Data is stored in a reactive `#items` source containing a Set, while `super()` is called without arguments (empty Set)

The `#init()` method creates implementations for `read_methods` and `set_like_methods` by delegating to `Set.prototype` methods. However, the code used `set_proto[method].apply(this, v)` where `this` is the SvelteSet instance. Since SvelteSet extends Set but never adds items to itself (all data is in `#items`), calling these Set methods on `this` operates on an empty Set.

This caused severe bugs:
- `forEach()` would iterate over nothing instead of actual items
- `isSubsetOf()` would always return `true` (empty set is subset of everything)
- `isSupersetOf()` would always return `false` (empty set can't be superset of non-empty)
- `union()` would only return the other set (union with empty set)
- `intersection()` would return empty (intersection with empty set)
- `difference()` would return empty
- `symmetricDifference()` would just return the other set's items

**The Fix:**

Changed `set_proto[method].apply(this, v)` to `set_proto[method].apply(get(this.#items), v)` in both loops.

This ensures the Set methods operate on the actual data stored in `#items` (the reactive Set that contains all the items) rather than the empty underlying Set that the SvelteSet class extends.

The fix maintains reactivity since `get(this.#items)` is still called, establishing the reactive dependency as intended.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
Base automatically changed from each-block-pending to main March 18, 2026 16:43
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