Skip to content
Myr

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

4 files+31 added−6 removed

modified:__tests__/patch.js+18 added
37 unmodified lines
38 })39}4041describe("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 lines
modified:src/immer.js+9 added-1 removed
1import * as legacyProxy from "./es5"2import * as modernProxy from "./proxy"3import {generatePatches} from "./patches"3import {applyPatches, generatePatches} from "./patches"4import {5 assign,6 each,7 has,
106 unmodified lines
114 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 lines
modified:src/index.js+3 added-4 removed
modified:src/patches.js+1 added-1 removed