Skip to content

Commit ad510d4

Browse files
committed
Re-add isDraftable check to skip freezing non-obj values
1 parent d6c1202 commit ad510d4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

__tests__/base.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,29 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
26762676
) // should throw!
26772677
})
26782678

2679+
autoFreeze &&
2680+
test("issue #1190 / #1192 - should not freeze non-draftable objects (class instances and typed arrays)", () => {
2681+
class MutableClass {
2682+
value = 5
2683+
}
2684+
2685+
const state = {
2686+
someValue: 5,
2687+
mutableClass: new MutableClass(),
2688+
typedArray: new Uint8Array(10)
2689+
}
2690+
2691+
expect(() => {
2692+
// Should not throw when producing with autoFreeze enabled
2693+
const result = produce(state, draft => {
2694+
draft.someValue = 6
2695+
})
2696+
2697+
// Verify the non-draftable class instance is not frozen
2698+
state.mutableClass.value = 6
2699+
}).not.toThrow()
2700+
})
2701+
26792702
autoFreeze &&
26802703
test("issue #469, state not frozen", () => {
26812704
const project = produce(

src/utils/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export function shallowCopy(base: any, strict: StrictMode) {
243243
*/
244244
export function freeze<T>(obj: T, deep?: boolean): T
245245
export function freeze<T>(obj: any, deep: boolean = false): T {
246-
if (isFrozen(obj) || isDraft(obj)) return obj
246+
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj
247247
if (getArchtype(obj) > 1 /* Map or Set */) {
248248
O.defineProperties(obj, {
249249
set: dontMutateMethodOverride,

0 commit comments

Comments
 (0)