Skip to content
Myr

fix: make sure produceWithPatches accepts promises

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by cviejo · committed by GitHub · parent e140918 · Browse files · 2 files+18 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−2 removed

  • tests:+12 added−0 removed
  • other:+6 added−2 removed

2 files+18 added−2 removed

modified:__tests__/base.js+12 added
1726 unmodified lines
1727 a: {b: {k: 1, c: 1}}1728 })1729 })1730 })17311732 it("works with patches", () =>1733 produceWithPatches({a: 0}, async d => {1734 await Promise.resolve()1735 d.a = 11736 }).then(([nextState, patches, inversePathes]) => {1737 expect(nextState).toEqual({a: 1})1738 expect(patches).toEqual([{op: "replace", path: ["a"], value: 1}])1739 expect(inversePathes).toEqual([1740 {op: "replace", path: ["a"], value: 0}1741 ])1742 }))1743 })17441745 it("throws when the draft is modified and another object is returned", () => {1746 const base = {x: 3}
723 unmodified lines
modified:src/core/immerClass.ts+6 added-2 removed
130 unmodified lines
131 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))132 }133134 let patches: Patch[], inversePatches: Patch[]135 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {135 const result = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {136 patches = p137 inversePatches = ip138 })139140 if (typeof Promise !== "undefined" && result instanceof Promise) {141 return result.then(nextState => [nextState, patches!, inversePatches!])142 }139 return [nextState, patches!, inversePatches!]143 return [result, patches!, inversePatches!]144 }145146 createDraft<T extends Objectish>(base: T): Draft<T> {147 if (!isDraftable(base)) die(8)
91 unmodified lines