Skip to content

Commit

Permalink
fix(es5): account for drafts in prepareCopy
Browse files Browse the repository at this point in the history
The `prepareCopy` function was not taking into account that the base state might be a draft.
  • Loading branch information
aleclarson committed Jan 21, 2019
1 parent 9877d64 commit 1e3e425
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,10 @@ export function willFinalize(result, baseDraft, needPatches) {
}

export function createDraft(base, parent) {
let draft
if (isDraft(base)) {
const state = base[DRAFT_STATE]
// Avoid creating new drafts when copying.
state.finalizing = true
draft = shallowCopy(state.draft, true)
state.finalizing = false
} else {
draft = shallowCopy(base)
}

const isArray = Array.isArray(base)
const draft = clonePotentialDraft(base)
eachOwn(draft, prop => {
const enumerable = isArray || isEnumerable(base, prop)
proxyProperty(draft, prop, enumerable)
proxyProperty(draft, prop, isArray || isEnumerable(base, prop))
})

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

function prepareCopy(state) {
if (!state.copy) state.copy = shallowCopy(state.base)
if (!state.copy) state.copy = clonePotentialDraft(state.base)
}

function clonePotentialDraft(base) {
const state = base && base[DRAFT_STATE]
if (state) {
state.finalizing = true
const draft = shallowCopy(state.draft, true)
state.finalizing = false
return draft
}
return shallowCopy(base)
}

function proxyProperty(draft, prop, enumerable) {
Expand Down

0 comments on commit 1e3e425

Please sign in to comment.