Repository navigation
fix(mobx): don't track observable set mutations in the running derivation - #4765
Merged
Merged
Conversation
…tion ObservableSet.add and delete checked membership through the public has(), which reports the set as observed. So a derivation that only writes to a set got subscribed to it, and replace went through the same path. Observable maps and arrays don't do this.
🦋 Changeset detectedLatest commit: c96dfa2 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 |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the change preventing observable set mutations from creating unintended subscriptions.
No open issues 🎉
What changed in this PR
This PR prevents observable set mutations from unintentionally subscribing the running derivation to the set, aligning their behavior with observable maps and arrays.
Changes:
- Adds a private, non-tracking membership check for
addanddelete, also fixing tracking throughreplace. - Adds regression tests for untracked mutations and verifies that public
has()remains reactive. - Adds a patch changeset documenting the fix.
| File | Description |
|---|---|
packages/mobx/src/types/observableset.ts |
Separates internal membership checks from public reactive reads. |
packages/mobx/__tests__/base/set.js |
Tests mutation tracking behavior and preserves has() reactivity. |
.changeset/observable-set-mutations-untracked.md |
Records the observable set fix for a patch release. |
Review effort: Balanced
Collaborator
|
Thanks, it seems the bug has been sitting there since observable sets were implemented |
Merged
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.
ObservableSet.addanddeletecheck membership through the publichas(), which reports the set as observed. So a derivation that only writes to a set gets subscribed to it, andreplacegoes through the same path. It hurts most withreplace, because the re-run undoes the other change:Observable maps and arrays don't do this:
map.setuses a privatehas_, and map.js already tests thatmap.clearisn't tracked. WithobservableRequiresReactionon, plainadd/deletecalls also warned that the set was being read outside a reactive context.This adds the same private
has_toObservableSet.has()itself still subscribes. Production cjs bundle: +22 bytes raw, -1 byte gzipped.Code change checklist
/docs. For new functionality, at leastAPI.mdshould be updatednpm -w mobx run test:performance)