fix: make SvelteSet work with async mode (alternative approach)#17903
Closed
Rich-Harris wants to merge 11 commits intoeach-block-pendingfrom
Closed
fix: make SvelteSet work with async mode (alternative approach)#17903Rich-Harris wants to merge 11 commits intoeach-block-pendingfrom
Rich-Harris wants to merge 11 commits intoeach-block-pendingfrom
Conversation
🦋 Changeset detectedLatest commit: 88293b6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
…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>
Member
Author
|
closing in favour of #17902, which I think has better trade-offs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Alternative to #17902
Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint