fix: applyPatches should mutate drafts
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This mimics the behavior of applyPatches before v1.11.0 Fixes #301
authored by aleclarson · committed by aleclarson · parent c89d47d · Browse files · 4 files·+31 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+31 added−6 removed
- tests:+18 added−0 removed
- other:+13 added−6 removed
Provenance
- Commit
d41be77- Tree
6772027- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
c89d47d
4 files changed+31 added−6 removed
modified:__tests__/patch.js+18 added
37 unmodified lines38 })39 }40 41 describe("applyPatches", () => {42+ it("mutates the base state when it is a draft", () => {43+ produce({a: 1}, draft => {44+ const result = applyPatches(draft, [45+ {op: "replace", path: ["a"], value: 2}46+ ])47+ expect(result).toBe(draft)48+ expect(draft.a).toBe(2)49+ })50+ })51+ it("produces a copy of the base state when not a draft", () => {52+ const base = {a: 1}53+ const result = applyPatches(base, [54+ {op: "replace", path: ["a"], value: 2}55+ ])56+ expect(result).not.toBe(base)57+ expect(result.a).toBe(2)58+ expect(base.a).toBe(1)59+ })60 it('throws when `op` is not "add", "replace", nor "remove"', () => {61 expect(() => {62 const patch = {op: "copy", from: [0], path: [1]}63 applyPatches([2], [patch])288 unmodified linesmodified:src/immer.js+9 added-1 removed
1 import * as legacyProxy from "./es5"2 import * as modernProxy from "./proxy"3-import {generatePatches} from "./patches"3+import {applyPatches, generatePatches} from "./patches"4 import {5 assign,6 each,7 has,106 unmodified lines114 setUseProxies(value) {115 this.useProxies = value116 assign(this, value ? modernProxy : legacyProxy)117 }118+ applyPatches(base, patches) {119+ // Mutate the base state when a draft is passed.120+ if (isDraft(base)) {121+ return applyPatches(base, patches)122+ }123+ // Otherwise, produce a copy of the base state.124+ return this.produce(base, draft => applyPatches(draft, patches))125+ }126 /**127 * @internal128 * Finalize a draft, returning either the unmodified base state or a modified129 * copy of the base state.100 unmodified linesmodified:src/index.js+3 added-4 removed
1123423 unmodified lines2829303133323334353637383941404142434445464847484950516 unmodified linesimport {applyPatches as applyPatchesImpl} from "./patches"import {Immer} from "./immer"
const immer = new Immer()
23 unmodified lines * Pass true to automatically freeze all copies created by Immer. * * By default, auto-freezing is disabled in production. */export const setAutoFreeze = value => immer.setAutoFreeze(value)export const setAutoFreeze = immer.setAutoFreeze.bind(immer)
/** * Pass true to use the ES2015 `Proxy` class when creating drafts, which is * always faster than using ES5 proxies. * * By default, feature detection is used, so calling this is rarely necessary. */export const setUseProxies = value => immer.setUseProxies(value)export const setUseProxies = immer.setUseProxies.bind(immer)
/** * Apply an array of Immer patches to the first argument. * * This function is a producer, which means copy-on-write is in effect. */export const applyPatches = produce(applyPatchesImpl)export const applyPatches = immer.applyPatches.bind(immer)
export { original, isDraft,6 unmodified linesmodified:src/patches.js+1 added-1 removed
4 unmodified lines5678991011121396 unmodified lines4 unmodified lines ? generateArrayPatches(state, basePath, patches, inversePatches) : generateObjectPatches(state, basePath, patches, inversePatches)}
export function generateArrayPatches(state, basePath, patches, inversePatches) {function generateArrayPatches(state, basePath, patches, inversePatches) { const {base, copy, assigned} = state const minLength = Math.min(base.length, copy.length)
// Look for replaced indices.96 unmodified lines