Skip to content
Myr

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

4 files+51 added−29 removed

modified:__tests__/frozen.js+24 added-26 removed
modified:src/common.js+9 added
118 unmodified lines
119 const cloned = Object.create(Object.getPrototypeOf(obj))120 for (const key in obj) cloned[key] = clone(obj[key])121 return cloned122}123124export 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 lines
10 isDraftable,11 isEnumerable,12 shallowCopy,13 DRAFT_STATE,14 NOTHING14 NOTHING,15 deepFreeze16} from "./common"17import {ImmerScope} from "./scope"1819function verifyMinified() {}
67 unmodified lines
87 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 lines
172 }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 lines
212 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 = true
82 unmodified lines
303 }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 }310311 if (isDraftProp && this.onAssign) {312 this.onAssign(state, prop, value)
2 unmodified lines
315316 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 lines
547## Auto freezing548549Immer 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.550551_⚠️ 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! ⚠️_552553## Immer on older JavaScript environments?554555By 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