Skip to content
Myr

common.js

This file

Path

src/common.js

77 lines · 2.5 KiB · JavaScript

Last changed by

Freeze non-proxied dataMichel Weststratebefore adoption5ac3ef7

History of this file

Take it

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

lines 1–33why, one step back
1export const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version
2export let autoFreeze = true34export function isProxy(value) {5 return !!value && !!value[PROXY_STATE]6}78export function isProxyable(value) {9 if (!value) return false10 if (typeof value !== "object") return false11 if (Array.isArray(value)) return true12 const proto = Object.getPrototypeOf(value)13 return proto === null || proto === Object.prototype14}1516export function freeze(value) {17 if (autoFreeze) {18 Object.freeze(value)19 }20 return value21}2223/**24 * Automatically freezes any state trees generated by immer.25 * This protects against accidental modifications of the state tree outside of an immer function.26 * This comes with a performance impact, so it is recommended to disable this option in production.27 * It is by default enabled.28 *29 * @returns {void}30 */31export function setAutoFreeze(enableAutoFreeze) {32 autoFreeze = enableAutoFreeze33}

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

line 34why, one step back
34

small improvementsMichel Weststratebefore adoption7d0b7e8

lines 35–38why, one step back
35export function shallowCopy(value) {
36 return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?37}38

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 39–46why, one step back
39export function each(value, cb) {
40 if (Array.isArray(value)) {41 for (let i = 0; i < value.length; i++) cb(i, value[i])42 } else {43 for (let key in value) cb(key, value[key])44 }45}46

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 47–66why, one step back
47export function finalizeNonProxiedObject(parent, finalizer) {
48 // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original49 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well50 if (!isProxyable(parent)) return51 if (Object.isFrozen(parent)) return52 if (Array.isArray(parent)) {53 for (let i = 0; i < parent.length; i++) {54 const child = parent[i]55 if (isProxy(child)) {56 parent[i] = finalizer(child)57 } else finalizeNonProxiedObject(child, finalizer)58 }59 } else {60 for (let key in parent) {61 const child = parent[key]62 if (isProxy(child)) {63 parent[key] = finalizer(child)64 } else finalizeNonProxiedObject(child, finalizer)65 }66 }

Freeze non-proxied dataMichel Weststratebefore adoption5ac3ef7

lines 67–68why, one step back
67 // always freeze completely new data
68 freeze(parent)

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 69–71why, one step back
69}
7071export function verifyReturnValue(value) {

Freeze non-proxied dataMichel Weststratebefore adoption5ac3ef7

line 72why, one step back
72 // values either than undefined will trigger warning;

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 73–76why, one step back
73 if (value !== undefined)
74 console.warn(75 `Immer callback expects no return value. However ${typeof value} was returned`76 )

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

line 77why, one step back
77}