Skip to content
Myr

Cleanup

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

authored by Michel Weststrate · committed by Michel Weststrate · parent fd76f1c · Browse files · 2 files+30 added−42 removed
This commit

State

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

Diff stats

2 files+30 added−42 removed

  • other:+30 added−42 removed

2 files+30 added−42 removed

modified:src/es5.ts+4 added-1 removed
223 unmodified lines
224 if (!state) return225 const {base, draft, assigned, type} = state226 if (type === ProxyType.ES5Object) {227 // Look for added keys.228 // TODO: looks quite duplicate to hasObjectChanges228 // TODO: looks quite duplicate to hasObjectChanges,229 // probably there is a faster way to detect changes, as sweep + recurse seems to do some230 // unnecessary work.231 // also: probably we can store the information we detect here, to speed up tree finalization!232 each(draft, key => {233 if ((key as any) === DRAFT_STATE) return234 // The `undefined` check is a fast path for pre-existing keys.235 if (base[key] === undefined && !has(base, key)) {
75 unmodified lines
modified:src/finalize.ts+26 added-41 removed
124 unmodified lines
125 state.copy = shallowCopy(state.draft, true)126 }127 root = state.copy128 }129 const needPatches = !!rootPath && !!scope.patches129 each(root, (key, value) =>131 finalizeProperty(132 immer,133 root,134 state,135 key,136 value,137 root,138 needPatches,139 scope,140 rootPath141 )130 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)131 )132 return root133}134135function finalizeProperty(147 // TODO: can do with less args?136 immer: Immer,149 root: ImmerState,150 state: ImmerState,151 prop: string | number,152 value: any,153 parent: Drafted,154 needPatches: boolean,137 scope: ImmerScope,138 root: Drafted,139 rootState: ImmerState,140 parentValue: Drafted,141 prop: string | number,142 childValue: any,143 rootPath?: PatchPath144) {158 if (value === parent) {145 if (childValue === parentValue) {146 throw Error("Immer forbids circular references")147 }148149 // In the `finalizeTree` method, only the `root` object may be a draft.163 const isDraftProp = !!state && parent === root164 const isSetMember = isSet(parent)150 const isDraftProp = !!rootState && parentValue === root151 const isSetMember = isSet(parentValue)152166 if (isDraft(value)) {153 if (isDraft(childValue)) {154 const path =155 rootPath &&156 isDraftProp &&169 needPatches &&157 !isSetMember && // Set objects are atomic since they have no keys.171 !has((state as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.158 !has((rootState as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.159 ? rootPath!.concat(prop)160 : undefined161162 // Drafts owned by `scope` are finalized here.176 value = finalize(immer, value, scope, path)177 set(parent, prop, value)163 childValue = finalize(immer, childValue, scope, path)164 set(parentValue, prop, childValue)165166 // Drafts from another scope must prevent auto-freezing.180 if (isDraft(value)) {167 if (isDraft(childValue)) {168 scope.canAutoFreeze = false169 }183184 // Unchanged drafts are never passed to the `onAssign` hook.185 // if (isDraftProp && !isSet && value === get(state.base, prop)) return170 }171 // Unchanged draft properties are ignored.188 else if (isDraftProp && is(value, get(state.base, prop))) {172 else if (isDraftProp && is(childValue, get(rootState.base, prop))) {173 return174 }175 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.192 else if (isDraftable(value) && !Object.isFrozen(value)) {193 each(value, (key, v) =>176 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?177 // especially the passing on of root and rootState doesn't make sense...178 else if (isDraftable(childValue) && !Object.isFrozen(childValue)) {179 each(childValue, (key, grandChild) =>180 finalizeProperty(181 immer,182 scope,183 root,197 state,184 rootState,185 childValue,186 key,199 v,200 value,201 needPatches,202 scope,187 grandChild,188 rootPath189 )190 )206 maybeFreeze(immer, value)191 maybeFreeze(immer, childValue)192 }193194 if (isDraftProp && immer.onAssign && !isSetMember) {210 immer.onAssign(state, prop, value)195 immer.onAssign(rootState, prop, childValue)196 }197}198199export function maybeFreeze(immer: Immer, value: any, deep = false) {
4 unmodified lines