Skip to content

Applying patches returns empty array after update to v1.11.0 #301

@hastebrot

Description

@hastebrot
  • Issue: Applying patches returns empty array after update to v1.11.0.

Works with immer v1.10.5. Does not work with immer v1.11.0 and v1.12.0.

Code to reproduce (in TypeScript):

import { produce, applyPatches } from "immer"

let patches = [] as any[]
const state0: { a: number, b?: number } = { a: 1 }
const state1 = produce(state0, (draft) => { draft.b = 9 }, (p) => { patches.push(...p) })
const state2 = produce(state1, (draft) => { draft.a = 3 }, (p) => { patches.push(...p) })
const state3 = produce(state2, (draft) => { draft.b = 99 }, (p) => { patches.push(...p) })
const state4 = produce(state3, (draft) => { draft.a = 5 }, (p) => { patches.push(...p) })

console.log(patches)
// [ { op: 'add', path: [ 'b' ], value: 9 },
//   { op: 'replace', path: [ 'a' ], value: 3 },
//   { op: 'replace', path: [ 'b' ], value: 99 },
//   { op: 'replace', path: [ 'a' ], value: 5 } ]

produce(state0, (draft) => { applyPatches(draft, patches) }, (p) => { patches = p })

console.log(patches)
// [ { op: 'add', path: [ 'b' ], value: 99 },
//   { op: 'replace', path: [ 'a' ], value: 5 } ]

console.log(state0)
// { a: 1 }
console.log(state4)
// { a: 5, b: 99 }

Code based on https://medium.com/@dedels/using-immer-to-compress-immer-patches-f382835b6c69.

Expected behavior: It logs the patches like in the comments.

Observed behavior: The second logged patches is an empty array [].

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions