BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #412 #363 #260 #230 #313 #229
authored by Michel Weststrate · committed by GitHub · parents ac61b8d, 9064d26 · Browse files · 4 files·+68 added−44 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+68 added−44 removed
- tests:+44 added−41 removed
- other:+24 added−3 removed
Provenance
- Commit
9fa2581- Tree
4d35c46- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
ac61b8d- Parent 2
9064d26
4 files changed+68 added−44 removed
modified:__tests__/frozen.js+44 added-41 removed
48 unmodified lines49505152535355565758596054555657585960616263636465666765666768697071727374757677787980818283848586878889749091929394959681979899848586878810010110210310410510610791929394959697989910010110210310410510810911011111210711311411511011111211311411511611711811912012111812212312412512 unmodified lines48 unmodified lines expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() }) })
describe("the result is never auto-frozen when", () => { it("the producer is a no-op", () => { const base = {} const next = produce(base, () => {}) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() it("a new object replaces the entire draft", () => { const obj = {a: {b: {}}} const next = produce({}, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeTruthy() })
it("the root draft is returned", () => { it("a new object is added to the root draft", () => { const base = {} const next = produce(base, draft => draft) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() const next = produce(base, draft => { draft.a = {b: []} }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.b)).toBeTruthy() })
it("a new object is added to a nested draft", () => { const base = {a: {}} const next = produce(base, draft => { draft.a.b = {c: {}} }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeTruthy() expect(isFrozen(next.a.b.c)).toBeTruthy() })
it("a nested draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft.a) expect(next).toBe(base.a) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next)).toBeTruthy() })
it("the base state is returned", () => { const base = {} const next = produce(base, () => base) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next)).toBeTruthy() })
it("a new object replaces a primitive base", () => { const obj = {} const next = produce(null, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeFalsy() it("the producer is a no-op", () => { const base = {a: {}} const next = produce(base, () => {}) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
it("a new object replaces the entire draft", () => { const obj = {a: {b: {}}} const next = produce({}, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next.a)).toBeFalsy() expect(isFrozen(next.a.b)).toBeFalsy() })
it("a new object is added to the root draft", () => { const base = {} const next = produce(base, draft => { draft.a = {} }) expect(next).not.toBe(base) it("the root draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeFalsy() expect(isFrozen(next.a)).toBeTruthy() })
it("a new object is added to a nested draft", () => { const base = {a: {}} const next = produce(base, draft => { draft.a.b = {} }) expect(next).not.toBe(base) it("a new object replaces a primitive base", () => { const obj = {a: {}} const next = produce(null, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeFalsy() }) })
it("can handle already frozen trees", () => {12 unmodified linesmodified:src/common.js+7 added
118 unmodified lines119 const cloned = Object.create(Object.getPrototypeOf(obj))120 for (const key in obj) cloned[key] = clone(obj[key])121 return cloned122 }123+124+export function deepFreeze(obj) {125+ if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return126+ Object.freeze(obj)127+ if (Array.isArray(obj)) obj.forEach(deepFreeze)128+ else for (const key in obj) deepFreeze(obj[key])129+}modified:src/immer.js+15 added-3 removed
9 unmodified lines10 isDraftable,11 isEnumerable,12 shallowCopy,13 DRAFT_STATE,14- NOTHING14+ NOTHING,15+ deepFreeze16 } from "./common"17 import {ImmerScope} from "./scope"18 19 function verifyMinified() {}67 unmodified lines87 scope.usePatches(patchListener)88 return this.processResult(result, scope)89 } else {90 result = recipe(base)90- if (result === undefined) return base91- return result !== NOTHING ? result : undefined91+ if (result === NOTHING) return undefined92+ if (result === undefined) result = base93+ this.maybeFreeze(result, true)94+ return result95 }96 }97 produceWithPatches(arg1, arg2, arg3) {98 if (typeof arg1 === "function") {73 unmodified lines172 }173 if (isDraftable(result)) {174 // Finalize the result in case it contains (or is) a subset of the draft.175 result = this.finalize(result, null, scope)176+ this.maybeFreeze(result)177 }178 if (scope.patches) {179 scope.patches.push({180 op: "replace",31 unmodified lines212 if (state.scope !== scope) {213 return draft214 }215 if (!state.modified) {216+ this.maybeFreeze(state.base, true)217 return state.base218 }219 if (!state.finalized) {220 state.finalized = true82 unmodified lines303 }304 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.305 else if (isDraftable(value) && !Object.isFrozen(value)) {306 each(value, finalizeProperty)307+ this.maybeFreeze(value)308 }309 310 if (isDraftProp && this.onAssign) {311 this.onAssign(state, prop, value)2 unmodified lines314 315 each(root, finalizeProperty)316 return root317 }318+ maybeFreeze(value, deep = false) {319+ if (this.autoFreeze && !isDraft(value)) {320+ if (deep) deepFreeze(value)321+ else Object.freeze(value)322+ }323+ }324 }modified:readme.md+2 added
546 unmodified lines547 ## Auto freezing548 549 Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. It is by default enabled. By default, it is turned on during local development and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.550 551+_⚠️ If auto freezing is enabled, recipes are not entirely side-effect free: Any plain object or array that ends up in the produced result, will be frozen, even when these objects were not frozen before the start of the producer! ⚠️_552+553 ## Immer on older JavaScript environments?554 555 By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native (< v0.59) on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.556 279 unmodified lines