Skip to content

Commit 13f88d8

Browse files
authored
fix: skip patches of a draft discarded by writing its base value back (#1345)
When a draft is modified and its base value is then assigned back to the same location, the draft is discarded and the produced state keeps the original value. `getPath` still accepted the base value as a sign that the draft was at its key, so the discarded draft's changes were emitted as patches, and replaying them modified a value that the produced state left untouched. A draft is stored in its parent's copy as soon as it is created, so the base value can only be back at that key when the original was written back. Only accept the draft itself or its copy.
1 parent 1b8ac6c commit 13f88d8

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎__tests__/patch.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,3 +1637,60 @@ describe("assigning a base value back over its own draft", () => {
16371637
{a: {child: {id: 1}, other: 1}}
16381638
)
16391639
})
1640+
1641+
describe("assigning a base value back over a modified draft", () => {
1642+
// Writing the base value back discards the draft, including its changes,
1643+
// so those changes must not show up in the patches either.
1644+
const objectChild = {id: 1}
1645+
runPatchTests(
1646+
"object",
1647+
{child: objectChild, other: 0},
1648+
d => {
1649+
d.child.id = 2
1650+
d.child = objectChild
1651+
d.other = 1
1652+
},
1653+
[{op: "replace", path: ["other"], value: 1}],
1654+
[{op: "replace", path: ["other"], value: 0}],
1655+
{child: {id: 1}, other: 1}
1656+
)
1657+
1658+
const nestedChild = {inner: {id: 1}}
1659+
runPatchTests(
1660+
"nested drafts",
1661+
{child: nestedChild},
1662+
d => {
1663+
d.child.inner.id = 2
1664+
d.child = nestedChild
1665+
},
1666+
[],
1667+
[],
1668+
{child: {inner: {id: 1}}}
1669+
)
1670+
1671+
const arrayChild = {id: 1}
1672+
runPatchTests(
1673+
"array",
1674+
{list: [arrayChild]},
1675+
d => {
1676+
d.list[0].id = 2
1677+
d.list[0] = arrayChild
1678+
},
1679+
[],
1680+
[],
1681+
{list: [{id: 1}]}
1682+
)
1683+
1684+
const mapChild = {id: 1}
1685+
runPatchTests(
1686+
"map",
1687+
{map: new Map([["child", mapChild]])},
1688+
d => {
1689+
d.map.get("child").id = 2
1690+
d.map.set("child", mapChild)
1691+
},
1692+
[],
1693+
[],
1694+
{map: new Map([["child", {id: 1}]])}
1695+
)
1696+
})

‎src/plugins/patches.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ export function enablePatches() {
6161
}
6262

6363
// Check if the value at the key is still related to this draft
64-
// It should be either the draft itself, the base, or the copy
65-
if (
66-
valueAtKey !== state.draft_ &&
67-
valueAtKey !== state.base_ &&
68-
valueAtKey !== state.copy_
69-
) {
64+
// It should be either the draft itself or the copy. The base means the
65+
// original value was written back, which discards this draft's changes
66+
if (valueAtKey !== state.draft_ && valueAtKey !== state.copy_) {
7067
return null // Value was replaced with something else
7168
}
7269
if (proxyDraft != null && proxyDraft.base_ !== state.base_) {

0 commit comments

Comments
 (0)