Skip to content
Myr

fix(es5): account for drafts in prepareCopy

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The `prepareCopy` function was not taking into account that the base state might be a draft.

authored by aleclarson · committed by Alec Larson · parent 9877d64 · Browse files · 1 file+14 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−14 removed

  • other:+14 added−14 removed

1 file+14 added−14 removed

modified:src/es5.js+14 added-14 removed
28 unmodified lines
29 }30}3132export function createDraft(base, parent) {33 let draft34 if (isDraft(base)) {35 const state = base[DRAFT_STATE]36 // Avoid creating new drafts when copying.37 state.finalizing = true38 draft = shallowCopy(state.draft, true)39 state.finalizing = false40 } else {41 draft = shallowCopy(base)42 }4333 const isArray = Array.isArray(base)34 const draft = clonePotentialDraft(base)35 eachOwn(draft, prop => {46 const enumerable = isArray || isEnumerable(base, prop)47 proxyProperty(draft, prop, enumerable)36 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))37 })3839 // See "proxy.js" for property documentation.40 const state = {
52 unmodified lines
93 }94}9596function prepareCopy(state) {108 if (!state.copy) state.copy = shallowCopy(state.base)97 if (!state.copy) state.copy = clonePotentialDraft(state.base)98}99100function clonePotentialDraft(base) {101 const state = base && base[DRAFT_STATE]102 if (state) {103 state.finalizing = true104 const draft = shallowCopy(state.draft, true)105 state.finalizing = false106 return draft107 }108 return shallowCopy(base)109}110111function proxyProperty(draft, prop, enumerable) {112 let desc = descriptors[prop]113 if (desc) {114 desc.enumerable = enumerable
121 unmodified lines