finalize.ts
This file
Path
src/finalize.ts
208 lines · 5.1 KiB · TypeScript
Last changed by
Small code cleanupMichel Weststratebefore adoption9217809
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 2–3why, one step back2 Immer,3 ImmerScope,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 4why, one step back4 DRAFT_STATE,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 5why, one step back5 isDraftable,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 6why, one step back6 NOTHING,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 7–11why, one step back7 Drafted,8 PatchPath,9 ProxyType,10 each,11 has,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 12why, one step back12 freeze,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 13why, one step back13 generatePatches,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 14why, one step back14 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 15–20why, one step back15 ImmerState,16 isSet,17 isDraft,18 SetState,19 set,20 is,Small code cleanupMichel Weststratebefore adoption9217809
lines 21–22why, one step back21 get,22 willFinalizefix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 23why, one step back23} from "./internal"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 24–27why, one step back2425export function processResult(immer: Immer, result: any, scope: ImmerScope) {26 const baseDraft = scope.drafts![0]27 const isReplaced = result !== undefined && result !== baseDraftSmall code cleanupMichel Weststratebefore adoption9217809
line 28why, one step back28 willFinalize(immer, scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 29–36why, one step back29 if (isReplaced) {30 if (baseDraft[DRAFT_STATE].modified) {31 scope.revoke()32 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore33 }34 if (isDraftable(result)) {35 // Finalize the result in case it contains (or is) a subset of the draft.36 result = finalize(immer, result, scope)fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54
line 37why, one step back37 if (!scope.parent) maybeFreeze(immer, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 38–182why, one step back38 }39 if (scope.patches) {40 scope.patches.push({41 op: "replace",42 path: [],43 value: result44 })45 scope.inversePatches!.push({46 op: "replace",47 path: [],48 value: baseDraft[DRAFT_STATE].base49 })50 }51 } else {52 // Finalize the base draft.53 result = finalize(immer, baseDraft, scope, [])54 }55 scope.revoke()56 if (scope.patches) {57 scope.patchListener!(scope.patches, scope.inversePatches!)58 }59 return result !== NOTHING ? result : undefined60}6162function finalize(63 immer: Immer,64 draft: Drafted,65 scope: ImmerScope,66 path?: PatchPath67) {68 const state = draft[DRAFT_STATE]69 if (!state) {70 if (Object.isFrozen(draft)) return draft71 return finalizeTree(immer, draft, scope)72 }73 // Never finalize drafts owned by another scope.74 if (state.scope !== scope) {75 return draft76 }77 if (!state.modified) {78 maybeFreeze(immer, state.base, true)79 return state.base80 }81 if (!state.finalized) {82 state.finalized = true83 finalizeTree(immer, state.draft, scope, path)8485 // We cannot really delete anything inside of a Set. We can only replace the whole Set.86 if (immer.onDelete && state.type !== ProxyType.Set) {87 // The `assigned` object is unreliable with ES5 drafts.88 if (immer.useProxies) {89 const {assigned} = state90 each(assigned, (prop, exists) => {91 if (!exists) immer.onDelete!(state, prop as any)92 })93 } else {94 const {base, copy} = state95 each(base, prop => {96 if (!has(copy, prop)) immer.onDelete!(state, prop as any)97 })98 }99 }100 if (immer.onCopy) {101 immer.onCopy(state)102 }103104 // At this point, all descendants of `state.copy` have been finalized,105 // so we can be sure that `scope.canAutoFreeze` is accurate.106 if (immer.autoFreeze && scope.canAutoFreeze) {107 freeze(state.copy, false)108 }109110 if (path && scope.patches) {111 generatePatches(state, path, scope.patches, scope.inversePatches!)112 }113 }114 return state.copy115}116117function finalizeTree(118 immer: Immer,119 root: Drafted,120 scope: ImmerScope,121 rootPath?: PatchPath122) {123 const state = root[DRAFT_STATE]124 if (state) {125 if (126 state.type === ProxyType.ES5Object ||127 state.type === ProxyType.ES5Array128 ) {129 // Create the final copy, with added keys and without deleted keys.130 state.copy = shallowCopy(state.draft, true)131 }132 root = state.copy133 }134 each(root, (key, value) =>135 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)136 )137 return root138}139140function finalizeProperty(141 immer: Immer,142 scope: ImmerScope,143 root: Drafted,144 rootState: ImmerState,145 parentValue: Drafted,146 prop: string | number,147 childValue: any,148 rootPath?: PatchPath149) {150 if (childValue === parentValue) {151 throw Error("Immer forbids circular references")152 }153154 // In the `finalizeTree` method, only the `root` object may be a draft.155 const isDraftProp = !!rootState && parentValue === root156 const isSetMember = isSet(parentValue)157158 if (isDraft(childValue)) {159 const path =160 rootPath &&161 isDraftProp &&162 !isSetMember && // Set objects are atomic since they have no keys.163 !has((rootState as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.164 ? rootPath!.concat(prop)165 : undefined166167 // Drafts owned by `scope` are finalized here.168 childValue = finalize(immer, childValue, scope, path)169 set(parentValue, prop, childValue)170171 // Drafts from another scope must prevent auto-freezing.172 if (isDraft(childValue)) {173 scope.canAutoFreeze = false174 }175 }176 // Unchanged draft properties are ignored.177 else if (isDraftProp && is(childValue, get(rootState.base, prop))) {178 return179 }180 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.181 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?182 // especially the passing on of root and rootState doesn't make sense...fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54
line 183why, one step back183 else if (isDraftable(childValue)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 184–195why, one step back184 each(childValue, (key, grandChild) =>185 finalizeProperty(186 immer,187 scope,188 root,189 rootState,190 childValue,191 key,192 grandChild,193 rootPath194 )195 )fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54
line 196why, one step back196 if (!scope.parent) maybeFreeze(immer, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 197–208why, one step back197 }198199 if (isDraftProp && immer.onAssign && !isSetMember) {200 immer.onAssign(rootState, prop, childValue)201 }202}203204export function maybeFreeze(immer: Immer, value: any, deep = false) {205 if (immer.autoFreeze && !isDraft(value)) {206 freeze(value, deep)207 }208}