Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
immerable,
nothing,
enablePatches,
enableMapSet
enableMapSet,
createDraft,
finishDraft
} from "../src/immer"

enablePatches()
Expand Down Expand Up @@ -790,6 +792,89 @@ runPatchTests(
]
)

// #1355
runPatchTests(
"arrays - truncate with length, then grow",
{x: [1]},
d => {
d.x.length = 0
d.x[1] = 2
},
[
{op: "replace", path: ["x", 0], value: undefined},
{op: "add", path: ["x", 1], value: 2}
],
[
{op: "replace", path: ["x", 0], value: 1},
{op: "remove", path: ["x", 1]}
],
{x: [undefined, 2]}
)

runPatchTests(
"arrays - truncate with length, then restore the length",
{x: [1, 2]},
d => {
d.x.length = 0
d.x.length = 2
},
[
{op: "replace", path: ["x", 0], value: undefined},
{op: "replace", path: ["x", 1], value: undefined}
],
[
{op: "replace", path: ["x", 0], value: 1},
{op: "replace", path: ["x", 1], value: 2}
],
{x: [undefined, undefined]}
)

runPatchTests(
"arrays - truncate with length, then push",
{x: [1, 2, 3]},
d => {
d.x.length = 1
d.x.push(4, 5)
},
[
{op: "replace", path: ["x", 1], value: 4},
{op: "replace", path: ["x", 2], value: 5}
],
[
{op: "replace", path: ["x", 1], value: 2},
{op: "replace", path: ["x", 2], value: 3}
],
{x: [1, 4, 5]}
)

runPatchTests(
"arrays - truncate with length, then grow back to the same values",
{x: [1, 2]},
d => {
d.x.length = 0
d.x[0] = 1
d.x[1] = 2
},
[],
[],
{x: [1, 2]}
)

test("arrays - truncate with length, then grow, on a draft from createDraft", () => {
// finishDraft is the first place a manual draft learns that patches are wanted
const draft = createDraft({x: [1]})
draft.x.length = 0
draft.x[1] = 2
let patches
finishDraft(draft, p => {
patches = p
})
expect(patches).toEqual([
{op: "replace", path: ["x", 0], value: undefined},
{op: "add", path: ["x", 1], value: 2}
])
})

runPatchTests(
"arrays - pop twice",
{x: [1, 2, 3]},
Expand Down
25 changes: 23 additions & 2 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
ENUMERABLE,
VALUE,
isArray,
isArrayIndex
isArrayIndex,
isPluginLoaded,
PluginPatches
} from "../internal"

interface ProxyBaseState extends ImmerBaseState {
Expand Down Expand Up @@ -284,7 +286,26 @@ arrayTraps.set = function (state, prop, value) {
isNaN(parseInt(prop as any))
)
die(14)
return objectTraps.set!.call(this, state[0], prop, value, state[0])
const arrayState = state[0]
// Only patch generation reads the dropped indices below; skip the work when
// patches are not enabled. (`scope_.patches_` is only set once the recipe
// has run, so it can't tell yet whether this call records patches.)
if (prop !== "length" || !isPluginLoaded(PluginPatches))
return objectTraps.set!.call(this, arrayState, prop, value, arrayState)
const oldLength = latest(arrayState).length
const result = objectTraps.set!.call(
this,
arrayState,
prop,
value,
arrayState
)
// Shrinking `length` drops the tail without going through `deleteProperty`,
// so mark the dropped base indices as assigned. Otherwise, if the array grows
// back, patch generation sees no assignment for them and emits no `replace`.
const end = Math.min(oldLength, arrayState.base_.length)
for (let i = +value; i < end; i++) arrayState.assigned_!.set("" + i, true)
return result
}

// Access a property without creating an Immer draft.
Expand Down