Skip to content

Commit

Permalink
fix: applyPatches should mutate drafts
Browse files Browse the repository at this point in the history
This mimics the behavior of applyPatches before v1.11.0

Fixes #301
  • Loading branch information
aleclarson committed Jan 23, 2019
1 parent c89d47d commit d41be77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
18 changes: 18 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ function runPatchTest(base, producer, patches, inversePathes) {
}

describe("applyPatches", () => {
it("mutates the base state when it is a draft", () => {
produce({a: 1}, draft => {
const result = applyPatches(draft, [
{op: "replace", path: ["a"], value: 2}
])
expect(result).toBe(draft)
expect(draft.a).toBe(2)
})
})
it("produces a copy of the base state when not a draft", () => {
const base = {a: 1}
const result = applyPatches(base, [
{op: "replace", path: ["a"], value: 2}
])
expect(result).not.toBe(base)
expect(result.a).toBe(2)
expect(base.a).toBe(1)
})
it('throws when `op` is not "add", "replace", nor "remove"', () => {
expect(() => {
const patch = {op: "copy", from: [0], path: [1]}
Expand Down
10 changes: 9 additions & 1 deletion src/immer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as legacyProxy from "./es5"
import * as modernProxy from "./proxy"
import {generatePatches} from "./patches"
import {applyPatches, generatePatches} from "./patches"
import {
assign,
each,
Expand Down Expand Up @@ -115,6 +115,14 @@ export class Immer {
this.useProxies = value
assign(this, value ? modernProxy : legacyProxy)
}
applyPatches(base, patches) {
// Mutate the base state when a draft is passed.
if (isDraft(base)) {
return applyPatches(base, patches)
}
// Otherwise, produce a copy of the base state.
return this.produce(base, draft => applyPatches(draft, patches))
}
/**
* @internal
* Finalize a draft, returning either the unmodified base state or a modified
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {applyPatches as applyPatchesImpl} from "./patches"
import {Immer} from "./immer"

const immer = new Immer()
Expand Down Expand Up @@ -30,22 +29,22 @@ export default produce
*
* 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,
Expand Down
2 changes: 1 addition & 1 deletion src/patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function generatePatches(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)

Expand Down

0 comments on commit d41be77

Please sign in to comment.