Implemented deepFreeze functionality, *anything draftable* returned from a produce will now be deeply frozen
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 8aa4da3 · Browse files · 4 files·+51 added−29 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+51 added−29 removed
- tests:+24 added−26 removed
- other:+27 added−3 removed
Provenance
- Commit
7fd3ef3- Tree
782903f- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
8aa4da3
4 files changed+51 added−29 removed
modified:__tests__/frozen.js+24 added-26 removed
49 unmodified lines5051525354555657585960615455565723 unmodified lines81828384938595868788899091929394959697989910010110210310010110410510610710810911011110810911211311411511211311411511611611711811912012112211811912012112212312412312412512611 unmodified lines49 unmodified lines expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
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() })
it("a new object replaces the entire draft", () => { const obj = {a: {b: {}}} const next = produce({}, () => obj) expect(next).toBe(obj)23 unmodified lines expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeTruthy() expect(isFrozen(next.a.b.c)).toBeTruthy() }) })
describe("the result is never auto-frozen when", () => { it("a nested draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft.a) expect(next).toBe(base.a) expect(isFrozen(next)).toBeTruthy() })
it("the base state is returned", () => { const base = {} const next = produce(base, () => base) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() })
it("the producer is a no-op", () => { const base = {a: {}} const next = produce(base, () => {}) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next.a)).toBeFalsy() expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
it("the root draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next.a)).toBeFalsy() expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).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() 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() })
it("the base state is returned", () => { const base = {} const next = produce(base, () => base) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() }) })
it("can handle already frozen trees", () => { const a = []11 unmodified linesmodified:src/common.js+9 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)) return126+ if (Object.isFrozen(obj)) return127+ if (isDraft(obj)) return128+ Object.freeze(obj)129+ if (Array.isArray(obj)) obj.forEach(deepFreeze)130+ for (const key in obj) deepFreeze(obj[key])131+}modified:src/immer.js+16 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+ // for externally incoming object trees, we wan't to make sure they're frozen automatically308+ this.maybeFreeze(value)309 }310 311 if (isDraftProp && this.onAssign) {312 this.onAssign(state, prop, value)2 unmodified lines315 316 each(root, finalizeProperty)317 return root318 }319+ maybeFreeze(value, deep = false) {320+ if (this.autoFreeze && !isDraft(value)) {321+ if (deep) deepFreeze(value)322+ else Object.freeze(value)323+ }324+ }325 }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