Skip to content

Commit

Permalink
fix: No patches being generated for root primitive replacements. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 11, 2022
1 parent d80e823 commit 0f96270
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
49 changes: 49 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,3 +1340,52 @@ test("#876 Ensure empty patch set for atomic set+delete on Map", () => {
expect(patches).toEqual([])
}
})

test("#888 patch to a primitive produces the primitive", () => {
{
const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
debugger
const [res, patches] = produceWithPatches(null, draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
const [res, patches] = produceWithPatches(0, draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
const [res, patches] = produceWithPatches("foobar", draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
const [res, patches] = produceWithPatches([], draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
const [res, patches] = produceWithPatches(false, draft => nothing)
expect(res).toEqual(undefined)
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
}
{
const [res, patches] = produceWithPatches(
"foobar",
draft => "something else"
)
expect(res).toEqual("something else")
expect(patches).toEqual([
{op: "replace", path: [], value: "something else"}
])
}
{
const [res, patches] = produceWithPatches(false, draft => true)
expect(res).toEqual(true)
expect(patches).toEqual([{op: "replace", path: [], value: true}])
}
})
2 changes: 1 addition & 1 deletion src/core/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function processResult(result: any, scope: ImmerScope) {
}
if (scope.patches_) {
getPlugin("Patches").generateReplacementPatches_(
baseDraft[DRAFT_STATE],
baseDraft[DRAFT_STATE].base_,
result,
scope.patches_,
scope.inversePatches_!
Expand Down
8 changes: 7 additions & 1 deletion src/core/immerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ export class Immer implements ProducersFns {
return processResult(result, scope)
} else if (!base || typeof base !== "object") {
result = recipe(base)
if (result === NOTHING) return undefined
if (result === undefined) result = base
if (result === NOTHING) result = undefined
if (this.autoFreeze_) freeze(result, true)
if (patchListener) {
const p: Patch[] = []
const ip: Patch[] = []
getPlugin("Patches").generateReplacementPatches_(base, result, p, ip)
patchListener(p, ip)
}
return result
} else die(21, base)
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function enablePatches() {
}

function generateReplacementPatches_(
rootState: ImmerState,
baseValue: any,
replacement: any,
patches: Patch[],
inversePatches: Patch[]
Expand All @@ -196,7 +196,7 @@ export function enablePatches() {
inversePatches.push({
op: REPLACE,
path: [],
value: rootState.base_
value: baseValue
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const plugins: {
inversePatches: Patch[]
): void
generateReplacementPatches_(
rootState: ImmerState,
base: any,
replacement: any,
patches: Patch[],
inversePatches: Patch[]
Expand Down

0 comments on commit 0f96270

Please sign in to comment.