Repository navigation
feat: add isModified helper - #1328
Open
saadkhantareen wants to merge 1 commit into
Open
saadkhantareen wants to merge 1 commit into
saadkhantareen wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Summary
Adds a public
isModified(value)helper, as proposed in #1153. Inside a recipe there is currently no cheap way to ask whether a draft subtree was changed: the available workarounds either snapshot the draft withcurrent()and compare it againstoriginal(), or finalize and re-create the draft.isModifiedanswers the question directly from the draft's internal state.Changes
src/utils/common.ts: implementisModified. It returns the draft state'smodified_flag, whichmarkChangedalready propagates from a modified nested draft to all of its parent drafts, so modifications anywhere inside the given draft are reported. Values that are not drafts returnfalse.src/immer.ts: exportisModifiedfrom the public entry point.src/types/index.js.flow: add the Flow declaration next toisDraft.website/docs/api.md: addisModifiedto the API overview table.__tests__/isModified.js: new tests covering non-draft values, unmodified drafts, direct property writes, additions and deletions, same-value assignments (not a modification), nested drafts and untouched siblings, arrays, manualcreateDraft/finishDraftdrafts, Map and Set drafts, and the derived-data use case from isModified helper #1153.Tests
npx vitest run __tests__/isModified.js --maxWorkers=2— 1 file passed, 10 tests passed.npx vitest run __tests__/current.js __tests__/original.js __tests__/base.js --maxWorkers=2— 3 files passed, 3367 tests passed.npx tsc -p tsconfig.json— no diagnostics insrc/; the only diagnostics reported are insidenode_modules/@types/nodeand are unchanged by this change.Fixes #1153