fix: #768 `immerable` field being lost during patch value cloning (#771)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by GitHub · parent 2c2f30e · Browse files · 4 files·+61 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+61 added−6 removed
- tests:+56 added−3 removed
- other:+5 added−3 removed
Provenance
- Commit
e0b7c01- Tree
15bf16e- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2c2f30e
4 files changed+61 added−6 removed
modified:__tests__/empty.ts+54 added-2 removed
1234556787910111213136 unmodified linesExpand all150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203import { produce, produceWithPatches, setUseProxies, enableAllPlugins enableAllPlugins, immerable, applyPatches} from "../src/immer"import {DRAFT_STATE} from "../src/internal"import {DRAFT_STATE, Patch} from "../src/internal"
enableAllPlugins()
test("empty stub test", () => {136 unmodified linesExpand all } } return data}
describe("#768", () => { class Stock { [immerable] = true
constructor(public price: number) {}
pushPrice(price: number) { this.price = price } }
type State = { stock: Stock }
test("bla", () => { // Set up conditions to produce the error const errorProducingPatch = [ { op: "replace", path: ["stock"], value: new Stock(200) } ] as Patch[]
// Start with modified state const state = { stock: new Stock(100) }
expect(state.stock.price).toEqual(100) expect(state.stock[immerable]).toBeTruthy() // Use patch to "replace" stocks debugger const resetState: State = applyPatches(state, errorProducingPatch) expect(state.stock.price).toEqual(100) expect(resetState.stock.price).toEqual(200) expect(resetState.stock[immerable]).toBeTruthy()
// Problems come in when resetState is modified const updatedState = produce(resetState, draft => { draft.stock.pushPrice(300) }) expect(state.stock.price).toEqual(100) expect(updatedState.stock.price).toEqual(300) expect(updatedState.stock[immerable]).toBeTruthy() expect(resetState.stock.price).toEqual(200) })})modified:__tests__/tsconfig.json+2 added-1 removed
1234566789{ "include": ["*", "../dist"], "compilerOptions": { "lib": ["es2015"], "strict": true, "noUnusedLocals": false "noUnusedLocals": false, "target": "ES5" }}modified:src/core/immerClass.ts+3 added-3 removed
183 unmodified lines184 }185 this.useProxies_ = value186 }187 188- applyPatches(base: Objectish, patches: Patch[]) {188+ applyPatches<T extends Objectish>(base: Objectish, patches: Patch[]): T {189 // If a patch replaces the entire state, take that replacement as base190 // before applying patches191 let i: number192 for (i = patches.length - 1; i >= 0; i--) {6 unmodified lines199 200 const applyPatchesImpl = getPlugin("Patches").applyPatches_201 if (isDraft(base)) {202 // N.B: never hits if some patch a replacement, patches are never drafts203- return applyPatchesImpl(base, patches)203+ return applyPatchesImpl(base, patches) as any204 }205 // Otherwise, produce a copy of the base state.206 return this.produce(base, (draft: Drafted) =>207 applyPatchesImpl(draft, patches.slice(i + 1))208- )208+ ) as any209 }210 }211 212 export function createProxy<T extends Objectish>(17 unmodified linesmodified:src/plugins/patches.ts+2 added
1+import {immerable} from "../immer"2 import {3 ImmerState,4 Patch,5 SetState,281 unmodified lines287 )288 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))289 const cloned = Object.create(Object.getPrototypeOf(obj))290 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])291+ if (has(obj, immerable)) cloned[immerable] = obj[immerable]292 return cloned293 }294 295 function clonePatchValueIfNeeded<T>(obj: T): T {11 unmodified lines