fix: No patches being generated for root primitive replacements. Fixes #888
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent d80e823 · Browse files · 5 files·+60 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+60 added−5 removed
- tests:+49 added−0 removed
- other:+11 added−5 removed
Provenance
- Commit
0f96270- Tree
2ca23f1- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
d80e823
5 files changed+60 added−5 removed
modified:__tests__/patch.js+49 added
1338 unmodified lines1339 })1340 expect(patches).toEqual([])1341 }1342 })1343+1344+test("#888 patch to a primitive produces the primitive", () => {1345+ {1346+ const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)1347+ expect(res).toEqual(undefined)1348+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1349+ }1350+ {1351+ debugger1352+ const [res, patches] = produceWithPatches(null, draft => nothing)1353+ expect(res).toEqual(undefined)1354+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1355+ }1356+ {1357+ const [res, patches] = produceWithPatches(0, draft => nothing)1358+ expect(res).toEqual(undefined)1359+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1360+ }1361+ {1362+ const [res, patches] = produceWithPatches("foobar", draft => nothing)1363+ expect(res).toEqual(undefined)1364+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1365+ }1366+ {1367+ const [res, patches] = produceWithPatches([], draft => nothing)1368+ expect(res).toEqual(undefined)1369+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1370+ }1371+ {1372+ const [res, patches] = produceWithPatches(false, draft => nothing)1373+ expect(res).toEqual(undefined)1374+ expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1375+ }1376+ {1377+ const [res, patches] = produceWithPatches(1378+ "foobar",1379+ draft => "something else"1380+ )1381+ expect(res).toEqual("something else")1382+ expect(patches).toEqual([1383+ {op: "replace", path: [], value: "something else"}1384+ ])1385+ }1386+ {1387+ const [res, patches] = produceWithPatches(false, draft => true)1388+ expect(res).toEqual(true)1389+ expect(patches).toEqual([{op: "replace", path: [], value: true}])1390+ }1391+})modified:src/core/finalize.ts+1 added-1 removed
35 unmodified lines36373839404041424344115 unmodified linesExpand all35 unmodified lines if (!scope.parent_) maybeFreeze(scope, result) } if (scope.patches_) { getPlugin("Patches").generateReplacementPatches_( baseDraft[DRAFT_STATE], baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_! )115 unmodified linesExpand allmodified:src/core/immerClass.ts+7 added-1 removed
112 unmodified lines113 usePatchesInScope(scope, patchListener)114 return processResult(result, scope)115 } else if (!base || typeof base !== "object") {116 result = recipe(base)117- if (result === NOTHING) return undefined117 if (result === undefined) result = base118+ if (result === NOTHING) result = undefined119 if (this.autoFreeze_) freeze(result, true)120+ if (patchListener) {121+ const p: Patch[] = []122+ const ip: Patch[] = []123+ getPlugin("Patches").generateReplacementPatches_(base, result, p, ip)124+ patchListener(p, ip)125+ }126 return result127 } else die(21, base)128 }129 115 unmodified linesmodified:src/plugins/patches.ts+2 added-2 removed
181 unmodified lines182 })183 }184 185 function generateReplacementPatches_(186- rootState: ImmerState,186+ baseValue: any,187 replacement: any,188 patches: Patch[],189 inversePatches: Patch[]190 ): void {4 unmodified lines195 })196 inversePatches.push({197 op: REPLACE,198 path: [],199- value: rootState.base_199+ value: baseValue200 })201 }202 203 function applyPatches_<T>(draft: T, patches: Patch[]): T {98 unmodified linesmodified:src/utils/plugins.ts+1 added-1 removed
19 unmodified lines20 patches: Patch[],21 inversePatches: Patch[]22 ): void23 generateReplacementPatches_(24- rootState: ImmerState,24+ base: any,25 replacement: any,26 patches: Patch[],27 inversePatches: Patch[]28 ): void81 unmodified lines