Skip to content

Commit 1e3e425

Browse files
committed
fix(es5): account for drafts in prepareCopy
The `prepareCopy` function was not taking into account that the base state might be a draft.
1 parent 9877d64 commit 1e3e425

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/es5.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,10 @@ export function willFinalize(result, baseDraft, needPatches) {
3030
}
3131

3232
export function createDraft(base, parent) {
33-
let draft
34-
if (isDraft(base)) {
35-
const state = base[DRAFT_STATE]
36-
// Avoid creating new drafts when copying.
37-
state.finalizing = true
38-
draft = shallowCopy(state.draft, true)
39-
state.finalizing = false
40-
} else {
41-
draft = shallowCopy(base)
42-
}
43-
4433
const isArray = Array.isArray(base)
34+
const draft = clonePotentialDraft(base)
4535
eachOwn(draft, prop => {
46-
const enumerable = isArray || isEnumerable(base, prop)
47-
proxyProperty(draft, prop, enumerable)
36+
proxyProperty(draft, prop, isArray || isEnumerable(base, prop))
4837
})
4938

5039
// See "proxy.js" for property documentation.
@@ -105,7 +94,18 @@ function markChanged(state) {
10594
}
10695

10796
function prepareCopy(state) {
108-
if (!state.copy) state.copy = shallowCopy(state.base)
97+
if (!state.copy) state.copy = clonePotentialDraft(state.base)
98+
}
99+
100+
function clonePotentialDraft(base) {
101+
const state = base && base[DRAFT_STATE]
102+
if (state) {
103+
state.finalizing = true
104+
const draft = shallowCopy(state.draft, true)
105+
state.finalizing = false
106+
return draft
107+
}
108+
return shallowCopy(base)
109109
}
110110

111111
function proxyProperty(draft, prop, enumerable) {

0 commit comments

Comments
 (0)