Cleanup. Don't recurse into frozen data structures
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 9f07525 · Browse files · 3 files·+28 added−26 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+28 added−26 removed
- tests:+18 added−0 removed
- other:+10 added−26 removed
Provenance
- Commit
d32cb97- Tree
3bcc8ed- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
9f07525
3 files changed+28 added−26 removed
modified:__tests__/base.js+18 added
1389 unmodified lines1390 1391 expect(world.inc(world).counter.count).toBe(2)1392 })1393 1394+ it("doesnt recurse into frozen structures if external data is frozen", () => {1395+ const frozen = {}1396+ Object.defineProperty(frozen, "x", {1397+ get() {1398+ throw "oops"1399+ },1400+ enumerable: true,1401+ configurable: true1402+ })1403+ Object.freeze(frozen)1404+1405+ expect(() => {1406+ produce({}, d => {1407+ d.x = frozen1408+ })1409+ }).not.toThrow()1410+ })1411+1412 // See here: https://github.com/mweststrate/immer/issues/891413 it("supports the spread operator", () => {1414 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}1415 const result = produce(base, draft => {705 unmodified linesmodified:src/finalize.ts+9 added-26 removed
20 unmodified lines21222324252627282962 unmodified lines929394959596979697989910010110210010110210316 unmodified lines1201211221231271281291301311321241251261361371271281291301311411321331341451461471481351361371381391521401411421561431441451591461471481631641651661491501511521536 unmodified lines20 unmodified lines 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 !== baseDraft62 unmodified lines return state.copy_}
function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) { // TODO: enable // if (Object.isFrozen(value)) // return value; // Don't recurse in tho recursive data structures 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 ) {16 unmodified lines childValue: any, rootPath?: PatchPath) { invariant(childValue !== targetObject, "Immer forbids circular references")
// In the `finalizeTree` method, only the `root` object may be a draft. 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. parentState && parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no 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. 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 // 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 } // Unchanged draft properties are ignored. if (isDraftProp && is(childValue, get(parentState!.base_, prop))) { if (parentState && is(childValue, get(parentState!.base_, prop))) { return } // TODO: cleanup conditions for reuse? // Search new objects for unfinalized drafts. Frozen objects should never contain drafts. if (isDraftable(childValue)) { finalizeTree(rootScope, childValue) // TODO: needed? // set(targetObject, prop, res) // TODO: drop first condition? // TODO: needed? // immer deep freezes plain objects, so if there is no parent state, we freeze as well if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue) }}6 unmodified linesmodified:notes.txt+1 added
17 unmodified lines18 [ ] check todos19 [ ] kill marchChangesSweep recursive if possible and no perf impact20 [ ] todo try immer perf on really big tree21 [ ] optimize each closures away?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 ┌───────────────────────┬───────────┬────────────┬───────────┐26 │ (index) │ just this │ cumulative │ increment │52 unmodified lines