Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions __tests__/frozen-finalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,54 @@ function runTests(name) {
expect(next.byId.moved).toBe(next.items[0])
})

it("leaves an external value mutable after deleting its draft", () => {
const payload = {count: 0}
const next = produce({child: {}}, draft => {
draft.child.payload = payload
delete draft.child
})
expect(next).toEqual({})
payload.count++
expect(payload.count).toBe(1)
})

it("only freezes values in drafts that remain in the result", () => {
const symbol = Symbol("kept")
const base = freeze({group: {first: {}, second: {}}, [symbol]: {}}, true)
const discarded = {inner: {}}
const moved = {inner: {}}
const kept = {inner: {}}
const next = produce(base, draft => {
draft.group.first.value = discarded
draft.group.second.value = moved
draft.wrapper = {child: draft.group.second}
draft[symbol].value = kept
delete draft.group
})
expect(isFrozen(discarded)).toBe(false)
expect(isFrozen(discarded.inner)).toBe(false)
expect(next.wrapper.child.value).toBe(moved)
expectDeepFrozen(next)
expectDeepFrozen(next[symbol])
})

it("leaves values mutable when their map and set entries are removed", () => {
const base = {map: new Map([[undefined, {}]]), set: new Set([{}])}
const fromMap = {}
const fromSet = {}
const next = produce(base, draft => {
draft.map.get(undefined).value = fromMap
for (const entry of draft.set) entry.value = fromSet
draft.map.clear()
draft.set.clear()
})
expect(next.map.size).toBe(0)
expect(next.set.size).toBe(0)
expectDeepFrozen(next)
expect(isFrozen(fromMap)).toBe(false)
expect(isFrozen(fromSet)).toBe(false)
})

it("deep freezes values returned from the recipe", () => {
const base = createFrozenBase()
const next = produce(base, draft => ({
Expand Down
48 changes: 43 additions & 5 deletions src/core/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function processResult(result: any, scope: ImmerScope) {
}

if (shouldAutoFreeze(scope)) {
freezeDraftCopies(scope)
freezeDraftCopies(scope, result)
freeze(result, true)
}

Expand Down Expand Up @@ -104,16 +104,17 @@ function finalize(rootScope: ImmerScope, value: any) {
let shouldAutoFreeze = (scope: ImmerScope) =>
!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_

// Freezes the copy of every modified draft once all finalization callbacks have run.
// Freezes the copy of each modified draft that remains in the result.
// Drafts are visited children first, so a copy is shallow-frozen and only the
// values written into it are deep-frozen. Values that came from a frozen base are
// already frozen, so the walk never descends into unchanged subtrees.
function freezeDraftCopies(scope: ImmerScope) {
const drafts = scope.drafts_
function freezeDraftCopies(scope: ImmerScope, result: any) {
const drafts = scope.drafts_.filter(state => state.modified_)
const discarded = getDiscardedCopies(scope, result, drafts)
for (let i = drafts.length - 1; i >= 0; i--) {
const state = drafts[i]
const {copy_} = state
if (!state.modified_ || !copy_ || isFrozen(copy_)) continue
if (!copy_ || isFrozen(copy_) || discarded?.has(copy_)) continue
if (!isFrozen(state.base_) || state.type_ === ArchType.Set) {
freeze(copy_, true)
continue
Expand All @@ -138,6 +139,43 @@ function freezeDraftCopies(scope: ImmerScope) {
}
}

// The usual case needs no tree walk: each modified copy still occupies its
// original slot, all the way to the root. If a draft was moved or removed,
// follow references from the result before freezing any of its copies.
function getDiscardedCopies(
scope: ImmerScope,
result: any,
drafts: ImmerState[]
) {
for (let i = 1; i < drafts.length; i++) {
const {parent_, key_, copy_} = drafts[i]
if (!parent_) continue
if (
parent_.type_ === ArchType.Set
? parent_.copy_!.has(copy_)
: get(parent_.copy_!, key_!, parent_.type_) === copy_
)
continue

// Only draft copies and new objects that were checked for nested drafts
// can contain another copy. Unchanged base subtrees need no inspection.
const unvisited = new Set(scope.handledSet_)
for (const draft of drafts) {
unvisited.add(draft.copy_)
}
function visit(_key: unknown, value: any) {
if (unvisited.delete(value)) {
const strict =
!scope.handledSet_?.has(value) ||
scope.immer_.shouldUseStrictIteration()
each(value, visit, strict)
}
}
visit(undefined, result)
return unvisited
}
}

function hasAssignedIndex(assigned: Map<unknown, boolean>) {
for (const [key, isAssigned] of assigned) {
if (isAssigned && key !== "length") return true
Expand Down