Skip to content
Myr

Made sure maps and sets become truly immutable

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 7d32b7c · Browse files · 3 files+47 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+47 added−7 removed

  • tests:+33 added−0 removed
  • other:+14 added−7 removed

3 files+47 added−7 removed

modified:__tests__/frozen.js+33 added
modified:src/common.js+11 added-3 removed
228 unmodified lines
229 for (const key in obj) cloned[key] = clone(obj[key])230 return cloned231}232233export function deepFreeze(obj) {233export function freeze(obj, deep = false) {234 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return235 if (isSet(obj)) {236 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections237 } else if (isMap(obj)) {238 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections239 }240 Object.freeze(obj)236 if (Array.isArray(obj)) obj.forEach(deepFreeze)237 else for (const key in obj) deepFreeze(obj[key])241 if (deep) each(obj, (_, value) => freeze(value, true))242}243244function dontMutateFrozenCollections() {245 throw new Error("This object has been frozen and should not be mutated")246}
modified:src/immer.js+3 added-4 removed
13 unmodified lines
14 isEnumerable,15 shallowCopy,16 DRAFT_STATE,17 NOTHING,18 deepFreeze18 freeze19} from "./common"20import {ImmerScope} from "./scope"2122function verifyMinified() {}
226 unmodified lines
249250 // At this point, all descendants of `state.copy` have been finalized,251 // so we can be sure that `scope.canAutoFreeze` is accurate.252 if (this.autoFreeze && scope.canAutoFreeze) {253 Object.freeze(state.copy)253 freeze(state.copy, false)254 }255256 if (path && scope.patches) {257 generatePatches(state, path, scope.patches, scope.inversePatches)
65 unmodified lines
323 return root324 }325 maybeFreeze(value, deep = false) {326 if (this.autoFreeze && !isDraft(value)) {327 if (deep) deepFreeze(value)328 else Object.freeze(value)327 freeze(value, deep)328 }329 }330}331
18 unmodified lines