Cleanup finalize, -57
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 d32cb97 · Browse files · 3 files·+55 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+55 added−51 removed
- other:+55 added−51 removed
Provenance
- Commit
530d81b- Tree
edd6142- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
d32cb97
3 files changed+55 added−51 removed
modified:src/finalize.ts+37 added-47 removed
1223457678910111213161415161718192021252223242530 unmodified lines5657585964656061626364656667686768697071727172737374757576777879808180818283848582838485868788899091929394959697989910010187888990102103104105959697989910010110210310410510610710810911011111211311410610710810916 unmodified lines126127128129139140130131132133134135136137148138139140141142143144155156145146147148149import { Immer, ImmerScope, DRAFT_STATE, isDraftable, NOTHING, Drafted, PatchPath, ProxyType, each, has, freeze, generatePatches, shallowCopy, ImmerState, isSet, isDraft, SetState, set, is, get, willFinalize} from "./internal"import invariant from "tiny-invariant"import {Archtype} from "./types-internal"
export function processResult(result: any, scope: ImmerScope) { const baseDraft = scope.drafts_![0] const isReplaced = result !== undefined && result !== baseDraft30 unmodified lines } return result !== NOTHING ? result : undefined}
function finalize(scope: ImmerScope, draft: Drafted, path?: PatchPath) { const state: ImmerState = draft[DRAFT_STATE]function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) { // Don't recurse in tho recursive data structures if (Object.isFrozen(value)) return value
const state: ImmerState = value[DRAFT_STATE] // A plain object, might need freezing, might contain drafts if (!state) { if (Object.isFrozen(draft)) return draft return finalizeTree(scope, draft) each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path) ) return value } // Never finalize drafts owned by another scope. if (state.scope_ !== scope) { return draft } if (state.scope_ !== rootScope) return value // Unmodified draft, return the (frozen) original if (!state.modified_) { maybeFreeze(scope, state.base_, true) maybeFreeze(rootScope, state.base_, true) return state.base_ } // Not finalized yet, let's do that now if (!state.finalized_) { state.finalized_ = true finalizeTree(scope, draft, path)
// At this point, all descendants of `state.copy` have been finalized, // so we can be sure that `scope.canAutoFreeze` is accurate. if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) { freeze(state.copy_, false) const result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys. state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array ? (state.copy_ = shallowCopy(state.draft_, true)) : state.copy_ // finalize all children of the copy each(result as any, (key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path) ) // everything inside is frozen, we can freeze here maybeFreeze(rootScope, result, false) // first time finalizing, let's create those patches if (path && rootScope.patches_) { generatePatches( state, path, rootScope.patches_, rootScope.inversePatches_! ) }
if (path && scope.patches_) { generatePatches(state, path, scope.patches_, scope.inversePatches_!) } } return state.copy_}
function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) { // Don't recurse in tho recursive data structures if (Object.isFrozen(value)) return value const state: ImmerState = value[DRAFT_STATE] if (state) { if ( state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array ) { // Create the final copy, with added keys and without deleted keys. state.copy_ = shallowCopy(state.draft_, true) } value = state.copy_ } each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, rootPath) ) return value}
function finalizeProperty( rootScope: ImmerScope, parentState: undefined | ImmerState, targetObject: any,16 unmodified lines // Drafts from another scope must prevented to be frozen // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze if (isDraft(res)) { rootScope.canAutoFreeze_ = false } return } else return } // Unchanged draft properties are ignored. if (parentState && is(childValue, get(parentState!.base_, prop))) { return } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts. if (isDraftable(childValue)) { finalizeTree(rootScope, childValue) finalize(rootScope, childValue) // immer deep freezes plain objects, so if there is no parent state, we freeze as well if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue) }}
export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) { if (scope.immer_.autoFreeze_ && !isDraft(value)) {export function maybeFreeze(scope: ImmerScope, value: any, deep = false) { if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) { freeze(value, deep) }}modified:src/immerClass.ts+2 added-2 removed
8 unmodified lines9 isDraftable,10 ImmerScope,11 processResult,12 NOTHING,13- maybeFreeze,13 Patch,14 Objectish,15 DRAFT_STATE,16 Draft,7 unmodified lines24 markChangedProxy,25 createProxyProxy26 } from "./internal"27 import invariant from "tiny-invariant"28+import {freeze} from "./common"29 30 declare const __DEV__: boolean31 /* istanbul ignore next */32 function verifyMinified() {}97 unmodified lines130 } else {131 result = recipe(base)132 if (result === NOTHING) return undefined133 if (result === undefined) result = base134- maybeFreeze({immer_: this}, result, true)134+ if (this.autoFreeze_) freeze(result, true)135 return result136 }137 }138 113 unmodified linesmodified:notes.txt+16 added-2 removed
3 unmodified lines4 [x] don't enable all features by default5 [ ] "undefined" "object" "function" prototype undefined + buildin FNs ?6 [x] kill hooks7 [x] _ postfix all members8-[ ] clean up finalize8+[x] clean up finalize9 [x] remove die10 [ ] own invariant11 [ ] optimize enumerations12 [ ] update docs4 unmodified lines17 [ ] kill isEnumerable?18 [ ] check todos19 [ ] kill marchChangesSweep recursive if possible and no perf impact20 [ ] todo try immer perf on really big tree21-[ ] optimize each closures away?21+[ ] optimize each closures away? pass in additional args?22 [ ] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue)23 24 Import size report for immer:25 ┌───────────────────────┬───────────┬────────────┬───────────┐49 unmodified lines75 │ enablePatches │ 4154 │ 5770 │ 691 │76 └───────────────────────┴───────────┴────────────┴───────────┘77 (this report was generated by npmjs.com/package/import-size)78 ]79+80+After finalize cleanup: [81+ Import size report for immer:82+┌───────────────────────┬───────────┬────────────┬───────────┐83+│ (index) │ just this │ cumulative │ increment │84+├───────────────────────┼───────────┼────────────┼───────────┤85+│ import * from 'immer' │ 5911 │ 0 │ 0 │86+│ produce │ 3397 │ 3397 │ 0 │87+│ enableES5 │ 4199 │ 4208 │ 811 │88+│ enableMapSet │ 4217 │ 5001 │ 793 │89+│ enablePatches │ 4091 │ 5693 │ 692 │90+└───────────────────────┴───────────┴────────────┴───────────┘91+(this report was generated by npmjs.com/package/import-size)92+]