Skip to content
Myr

common.js

This file

Path

src/common.js

33 lines · 1.0 KiB · JavaScript

Last changed by

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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}