Initial finalize 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 c2d7617 · Browse files · 4 files·+49 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+49 added−39 removed
- tests:+5 added−0 removed
- other:+44 added−39 removed
Provenance
- Commit
67ab3ab- Tree
c8ed9c9- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
c2d7617
4 files changed+49 added−39 removed
modified:__tests__/base.js+4 added
21 unmodified lines22 23 runBaseTest("proxy (no freeze)", true, false)24 runBaseTest("proxy (autofreeze)", true, true)25 runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)26+// runBaseTest("proxy (patch listener)", true, ufalse, true)27+28+// TODO: no freeze + patch29 runBaseTest("es5 (no freeze)", false, false)30 runBaseTest("es5 (autofreeze)", false, true)31 runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)32+// TODO: no freeze + patch33 34 function runBaseTest(name, useProxies, autoFreeze, useListener) {35 const listener = useListener ? function() {} : undefined36 const {produce, produceWithPatches} = createPatchedImmer({2067 unmodified linesmodified:__tests__/frozen.js+1 added
21 unmodified lines22 it("never freezes the base state", () => {23 const base = {arr: [1], obj: {a: 1}}24 const next = produce(base, draft => {25 draft.arr.push(1)26+ debugger27 })28 expect(isFrozen(base)).toBeFalsy()29 expect(isFrozen(base.arr)).toBeFalsy()30 expect(isFrozen(next)).toBeTruthy()185 unmodified linesmodified:src/finalize.ts+43 added-39 removed
32 unmodified lines333435363737383940418 unmodified lines505152535454555657585960616263636465666767686970713 unmodified lines757677787979808182836 unmodified lines9091929394959495969798991001011021031041051061071081091041101111061071121131141091151161171181131141151161191201211221231241251211261271281241251291301311321331341351361371321381391401411421371381431441451461471481411421491501511521531541551461561571581591601501511521531541551561571581591601611621631641611621631641651661671681691701711724 unmodified lines32 unmodified lines invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore } if (isDraftable(result)) { // Finalize the result in case it contains (or is) a subset of the draft. result = finalize(result, scope) result = finalize(scope, result) if (!scope.parent_) maybeFreeze(scope, result) } if (scope.patches_) { scope.patches_.push({8 unmodified lines }) } } else { // Finalize the base draft. result = finalize(baseDraft, scope, []) result = finalize(scope, baseDraft, []) } scope.revoke_() if (scope.patches_) { scope.patchListener_!(scope.patches_, scope.inversePatches_!) } return result !== NOTHING ? result : undefined}
function finalize(draft: Drafted, scope: ImmerScope, path?: PatchPath) {function finalize(scope: ImmerScope, draft: Drafted, path?: PatchPath) { const state: ImmerState = draft[DRAFT_STATE] if (!state) { if (Object.isFrozen(draft)) return draft return finalizeTree(draft, scope) return finalizeTree(scope, draft) } // Never finalize drafts owned by another scope. if (state.scope_ !== scope) { return draft3 unmodified lines return state.base_ } if (!state.finalized_) { state.finalized_ = true finalizeTree(draft, scope, path) 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_) {6 unmodified lines } return state.copy_}
function finalizeTree(root: Drafted, scope: ImmerScope, rootPath?: PatchPath) { const state: ImmerState = root[DRAFT_STATE]function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) { // TODO: enable // if (Object.isFrozen(value)) // return value; const state: ImmerState = value[DRAFT_STATE] if (state) { // if (rootScope !== state.scope_) // // Drafts from another scope must prevent auto-freezing. // state.scope_.canAutoFreeze_ = false; 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) } root = state.copy_ value = state.copy_ } each(root, (key, value) => finalizeProperty(scope, root, state, root, key, value, rootPath) each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, rootPath) ) return root return value}
function finalizeProperty( scope: ImmerScope, root: Drafted, rootState: ImmerState, parentValue: Drafted, rootScope: ImmerScope, parentState: undefined | ImmerState, targetObject: any, prop: string | number, childValue: any, rootPath?: PatchPath) { invariant(childValue !== parentValue, "Immer forbids circular references") invariant(childValue !== targetObject, "Immer forbids circular references")
// In the `finalizeTree` method, only the `root` object may be a draft. const isDraftProp = !!rootState && parentValue === root const isSetMember = isSet(parentValue) const isDraftProp = !!parentState const isSetMember = isSet(targetObject) // TODO: move inside if
// if the child is a draft, we can build a more precise patch if (isDraft(childValue)) { const path = rootPath && isDraftProp && !isSetMember && // Set objects are atomic since they have no keys. !has((rootState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys. !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys. ? rootPath!.concat(prop) : undefined
// Drafts owned by `scope` are finalized here. childValue = finalize(childValue, scope, path) set(parentValue, prop, childValue) const res = finalize(rootScope, childValue, path) set(targetObject, prop, res)
// Drafts from another scope must prevent auto-freezing. // TODO: weird place for this condition // if we got a draft back, we're in a nested thing and shouldn't freese if (isDraft(childValue)) { scope.canAutoFreeze_ = false if (isDraft(res)) { rootScope.canAutoFreeze_ = false }
return } // Unchanged draft properties are ignored. else if (isDraftProp && is(childValue, get(rootState.base_, prop))) { if (isDraftProp && is(childValue, get(parentState!.base_, prop))) { return } // TODO: cleanup conditions for reuse? // Search new objects for unfinalized drafts. Frozen objects should never contain drafts. // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion? // especially the passing on of root and rootState doesn't make sense... else if (isDraftable(childValue)) { each(childValue, (key, grandChild) => finalizeProperty( scope, root, rootState, childValue, key, grandChild, rootPath ) ) if (!scope.parent_) maybeFreeze(scope, childValue) if (isDraftable(childValue)) { finalizeTree(rootScope, childValue) // TODO: needed? // set(targetObject, prop, res) // TODO: drop first condition? // TODO: needed? if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue) }}
export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) {4 unmodified linesmodified:notes.txt+1 added
16 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?22 23 Import size report for immer:24 ┌───────────────────────┬───────────┬────────────┬───────────┐25 │ (index) │ just this │ cumulative │ increment │52 unmodified lines