Small improvements
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 746dc6f · Browse files · 4 files·+24 added−34 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+24 added−34 removed
- other:+24 added−34 removed
Provenance
- Commit
712af5c- Tree
e016e05- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
746dc6f
4 files changed+24 added−34 removed
modified:src/plugins/es5.ts+1 added
35 unmodified lines36 if (scope.patches_) {37 markChangesRecursively(scope.drafts_![0])38 }39 // This is faster when we don't care about which attributes changed.40+ // TODO: should be prefixed with else!41 markChangesSweep(scope.drafts_)42 }43 // When a child draft is returned, look for changes.44 else if (255 unmodified linesmodified:src/finalize.ts+16 added-28 removed
21 unmodified lines222324252626272829293031323334353637383738394041427 unmodified lines505152535454555657585960616263646566676863646566726768697071727379747576777884798081828883848586872 unmodified lines909192939910010110210310494959697984 unmodified lines10310410510611710710810911011111212311311411511616 unmodified lines1331341351361481371381391401419 unmodified lines15115215315416615515615715815916016116216317616416516616718018116816917017117221 unmodified lines willFinalize} from "./internal"import invariant from "tiny-invariant"
export function processResult(immer: Immer, result: any, scope: ImmerScope) {export function processResult(result: any, scope: ImmerScope) { const baseDraft = scope.drafts_![0] const isReplaced = result !== undefined && result !== baseDraft willFinalize(immer, scope, result, isReplaced) willFinalize(scope, result, isReplaced) if (isReplaced) { if (baseDraft[DRAFT_STATE].modified_) { scope.revoke_() 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(immer, result, scope) if (!scope.parent_) maybeFreeze(immer, result) result = finalize(result, scope) if (!scope.parent_) maybeFreeze(scope, result) } if (scope.patches_) { scope.patches_.push({ op: "replace",7 unmodified lines }) } } else { // Finalize the base draft. result = finalize(immer, baseDraft, scope, []) result = finalize(baseDraft, scope, []) } scope.revoke_() if (scope.patches_) { scope.patchListener_!(scope.patches_, scope.inversePatches_!) } return result !== NOTHING ? result : undefined}
function finalize( immer: Immer, draft: Drafted, scope: ImmerScope, path?: PatchPath) {function finalize(draft: Drafted, scope: ImmerScope, path?: PatchPath) { const state: ImmerState = draft[DRAFT_STATE] if (!state) { if (Object.isFrozen(draft)) return draft return finalizeTree(immer, draft, scope) return finalizeTree(draft, scope) } // Never finalize drafts owned by another scope. if (state.scope_ !== scope) { return draft } if (!state.modified_) { maybeFreeze(immer, state.base_, true) maybeFreeze(scope, state.base_, true) return state.base_ } if (!state.finalized_) { state.finalized_ = true finalizeTree(immer, state.draft_, scope, path) finalizeTree(draft, scope, path)
// At this point, all descendants of `state.copy` have been finalized, // so we can be sure that `scope.canAutoFreeze` is accurate. if (immer.autoFreeze_ && scope.canAutoFreeze_) { if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) { freeze(state.copy_, false) }
if (path && scope.patches_) {2 unmodified lines } return state.copy_}
function finalizeTree( immer: Immer, root: Drafted, scope: ImmerScope, rootPath?: PatchPath) {function finalizeTree(root: Drafted, scope: ImmerScope, rootPath?: PatchPath) { const state: ImmerState = root[DRAFT_STATE] if (state) { if ( state.type_ === ProxyType.ES5Object ||4 unmodified lines } root = state.copy_ } each(root, (key, value) => finalizeProperty(immer, scope, root, state, root, key, value, rootPath) finalizeProperty(scope, root, state, root, key, value, rootPath) ) return root}
function finalizeProperty( immer: Immer, scope: ImmerScope, root: Drafted, rootState: ImmerState, parentValue: Drafted,16 unmodified lines ? rootPath!.concat(prop) : undefined
// Drafts owned by `scope` are finalized here. childValue = finalize(immer, childValue, scope, path) childValue = finalize(childValue, scope, path) set(parentValue, prop, childValue)
// Drafts from another scope must prevent auto-freezing. if (isDraft(childValue)) {9 unmodified lines // especially the passing on of root and rootState doesn't make sense... else if (isDraftable(childValue)) { each(childValue, (key, grandChild) => finalizeProperty( immer, scope, root, rootState, childValue, key, grandChild, rootPath ) ) if (!scope.parent_) maybeFreeze(immer, childValue) if (!scope.parent_) maybeFreeze(scope, childValue) }}
export function maybeFreeze(immer: Immer, value: any, deep = false) { if (immer.autoFreeze_ && !isDraft(value)) {export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) { if (scope.immer_.autoFreeze_ && !isDraft(value)) { freeze(value, deep) }}modified:src/immerClass.ts+5 added-6 removed
115 unmodified lines116 if (typeof Promise !== "undefined" && result instanceof Promise) {117 return result.then(118 result => {119 scope.usePatches_(patchListener)120- return processResult(this, result, scope)120+ return processResult(result, scope)121 },122 error => {123 scope.revoke_()124 throw error125 }126 )127 }128 scope.usePatches_(patchListener)129- return processResult(this, result, scope)129+ return processResult(result, scope)130 } else {131 result = recipe(base)132 if (result === NOTHING) return undefined133 if (result === undefined) result = base134- maybeFreeze(this, result, true)134+ maybeFreeze({immer_: this}, result, true)135 return result136 }137 }138 30 unmodified lines169 invariant(state && state.isManual_, "First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore170 invariant(!state.finalized_, "The given draft is already finalized") // prettier-ignore171 const {scope_: scope} = state172 scope.usePatches_(patchListener)173- return processResult(this, undefined, scope)173+ return processResult(undefined, scope)174 }175 176 /**177 * Pass true to automatically freeze all copies created by Immer.56 unmodified lines234 return draft235 }236 237 export function willFinalize(238- immer: Immer,238 scope: ImmerScope,239 thing: any,240 isReplaced: boolean241 ) {243- if (!immer.useProxies_) willFinalizeES5(scope, thing, isReplaced)242+ if (!scope.immer_.useProxies_) willFinalizeES5(scope, thing, isReplaced)243 }244 245 export function markChanged(immer: Immer, state: ImmerState) {246 if (immer.useProxies_) {5 unmodified linesmodified:notes.txt+2 added
14 unmodified lines15 [ ] verify performance16 [ ] kill ownKeys?17 [ ] kill isEnumerable?18 [ ] check todos19+[ ] kill marchChangesSweep recursive if possible and no perf impact20+[ ] todo try immer perf on really big tree21 22 Import size report for immer:23 ┌───────────────────────┬───────────┬────────────┬───────────┐24 │ (index) │ just this │ cumulative │ increment │52 unmodified lines