fix: make SvelteSet work with async mode#17902
Open
Rich-Harris wants to merge 40 commits intomainfrom
Open
Conversation
🦋 Changeset detectedLatest commit: d7d7c31 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 |
6 tasks
Contributor
|
…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>
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.
Follow-up to #17162. It turns out
SvelteSetdidn'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
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint