Skip to content

Commit d53457f

Browse files
authored
fix: rebuild Set snapshots without mutating the traversal (#1325)
1 parent 47c66c9 commit d53457f

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

‎__tests__/current.js‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import {vi} from "vitest"
12
import {
3+
Immer,
24
setAutoFreeze,
35
current,
46
immerable,
@@ -244,3 +246,119 @@ function runTests(name) {
244246
})
245247
})
246248
}
249+
250+
// Bound Set traversal so a regression cannot hang the test process.
251+
function takeSetSnapshot(draft) {
252+
const forEach = Set.prototype.forEach
253+
const guard = vi
254+
.spyOn(Set.prototype, "forEach")
255+
.mockImplementation(function (callback, thisArg) {
256+
const size = this.size
257+
let visited = 0
258+
return forEach.call(this, (value, key, set) => {
259+
if (++visited > size)
260+
throw new Error("Set snapshot traversal grew beyond its initial size")
261+
callback.call(thisArg, value, key, set)
262+
})
263+
})
264+
try {
265+
return current(draft)
266+
} finally {
267+
guard.mockRestore()
268+
}
269+
}
270+
271+
describe.each([true, false])(
272+
"current Set snapshots - autoFreeze=%s",
273+
autoFreeze => {
274+
const {produce} = new Immer({autoFreeze})
275+
276+
it("snapshots modified objects in insertion order without leaking drafts", () => {
277+
const changed = {count: 0}
278+
const untouched = {count: 10}
279+
const base = new Set([changed, "middle", untouched])
280+
let snapshot
281+
const next = produce(base, draft => {
282+
const item = draft.values().next().value
283+
item.count = 1
284+
snapshot = takeSetSnapshot(draft)
285+
const values = [...snapshot]
286+
expect(values).toEqual([{count: 1}, "middle", untouched])
287+
expect(values[2]).toBe(untouched)
288+
expect(values.every(value => !isDraft(value))).toBe(true)
289+
expect(Object.isFrozen(snapshot)).toBe(false)
290+
expect(Object.isFrozen(values[0])).toBe(false)
291+
item.count = 2
292+
expect(values[0].count).toBe(1)
293+
})
294+
expect([...snapshot]).toEqual([{count: 1}, "middle", untouched])
295+
expect([...next]).toEqual([{count: 2}, "middle", untouched])
296+
expect(changed.count).toBe(0)
297+
})
298+
299+
it("snapshots object Sets nested in a parent", () => {
300+
const base = {items: new Set([{count: 0}])}
301+
let snapshot
302+
const next = produce(base, draft => {
303+
const item = draft.items.values().next().value
304+
item.count = 1
305+
snapshot = takeSetSnapshot(draft)
306+
item.count = 2
307+
})
308+
expect([...snapshot.items]).toEqual([{count: 1}])
309+
expect([...next.items]).toEqual([{count: 2}])
310+
expect([...base.items]).toEqual([{count: 0}])
311+
})
312+
313+
it("keeps membership snapshots independent of later additions and deletions", () => {
314+
const removed = {id: "removed"}
315+
const kept = {id: "kept"}
316+
const base = new Set([removed, kept])
317+
let snapshot
318+
const next = produce(base, draft => {
319+
draft.delete(removed)
320+
draft.add({id: "added"})
321+
snapshot = takeSetSnapshot(draft)
322+
expect([...snapshot]).toEqual([kept, {id: "added"}])
323+
expect([...snapshot][0]).toBe(kept)
324+
draft.clear()
325+
})
326+
expect([...snapshot]).toEqual([kept, {id: "added"}])
327+
expect(next.size).toBe(0)
328+
expect([...base]).toEqual([removed, kept])
329+
})
330+
331+
it("snapshots arrays stored as Set values", () => {
332+
const base = new Set([[1]])
333+
let snapshot
334+
produce(base, draft => {
335+
const item = draft.values().next().value
336+
item.push(2)
337+
snapshot = takeSetSnapshot(draft)
338+
item.push(3)
339+
})
340+
expect([...snapshot]).toEqual([[1, 2]])
341+
expect([...base]).toEqual([[1]])
342+
})
343+
344+
it("keeps unchanged frozen objects exactly once when adding a primitive", () => {
345+
const item = freeze({count: 0})
346+
const base = new Set([item])
347+
let snapshot
348+
produce(base, draft => {
349+
draft.add(1)
350+
snapshot = takeSetSnapshot(draft)
351+
})
352+
expect([...snapshot]).toEqual([item, 1])
353+
expect([...snapshot][0]).toBe(item)
354+
})
355+
356+
it("returns the original Set when its objects are only read", () => {
357+
const base = new Set([{count: 0}])
358+
produce(base, draft => {
359+
expect(draft.values().next().value.count).toBe(0)
360+
expect(takeSetSnapshot(draft)).toBe(base)
361+
})
362+
})
363+
}
364+
)

‎src/core/current.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
set,
99
ImmerState,
1010
isDraftable,
11-
isFrozen
11+
isFrozen,
12+
isSet
1213
} from "../internal"
1314

1415
/** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */
@@ -32,9 +33,12 @@ function currentImpl(value: any): any {
3233
} else {
3334
copy = shallowCopy(value, true)
3435
}
36+
// Rebuild Sets so snapshots replace their draft values in insertion order.
37+
const source = copy
38+
if (isSet(copy)) copy = new Set()
3539
// recurse
3640
each(
37-
copy,
41+
source,
3842
(key, childValue) => {
3943
set(copy, key, currentImpl(childValue))
4044
},

0 commit comments

Comments
 (0)