common.js
This file
Path
src/common.js
122 lines · 3.5 KiB · JavaScript
Last changed by
Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
History of this fileTake it
Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670
lines 1–4why, one step back1export const PROXY_STATE =2 typeof Symbol !== "undefined"3 ? Symbol("immer-proxy-state")4 : "__$immer_state"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 5why, one step back5Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
lines 6–12why, one step back6function verifyMinified() {}78const inProduction =9 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||10 verifyMinified.name === "verifyMinified"11let autoFreeze = !inProduction12Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 13–34why, one step back13let useProxies = typeof Proxy !== "undefined"1415/**16 * Automatically freezes any state trees generated by immer.17 * This protects against accidental modifications of the state tree outside of an immer function.18 * This comes with a performance impact, so it is recommended to disable this option in production.19 * It is by default enabled.20 *21 * @returns {void}22 */23export function setAutoFreeze(enableAutoFreeze) {24 autoFreeze = enableAutoFreeze25}2627export function setUseProxies(value) {28 useProxies = value29}3031export function getUseProxies() {32 return useProxies33}34Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 35–53why, one step back35export function isProxy(value) {36 return !!value && !!value[PROXY_STATE]37}3839export function isProxyable(value) {40 if (!value) return false41 if (typeof value !== "object") return false42 if (Array.isArray(value)) return true43 const proto = Object.getPrototypeOf(value)44 return proto === null || proto === Object.prototype45}4647export function freeze(value) {48 if (autoFreeze) {49 Object.freeze(value)50 }51 return value52}53Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 54–55why, one step back54export function shallowCopy(value) {55 return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 56why, one step back56}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 57why, one step back57Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 58–72why, one step back58export function each(value, cb) {59 if (Array.isArray(value)) {60 for (let i = 0; i < value.length; i++) cb(i, value[i])61 } else {62 for (let key in value) cb(key, value[key])63 }64}6566// given a base object, returns it if unmodified, or return the changed cloned if modified67export function finalize(base) {68 if (isProxy(base)) {69 const state = base[PROXY_STATE]70 if (state.modified === true) {71 if (state.finalized === true) return state.copy72 state.finalized = trueMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 73–79why, one step back73 return finalizeObject(74 useProxies ? state.copy : (state.copy = shallowCopy(base)),75 state76 )77 } else {78 return state.base79 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 80why, one step back80 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 81why, one step back81 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 82–84why, one step back82 return base83}84Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 85–92why, one step back85function finalizeObject(copy, state) {86 const base = state.base87 each(copy, (prop, value) => {88 if (value !== base[prop]) copy[prop] = finalize(value)89 })90 return freeze(copy)91}92Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 93why, one step back93function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 94–97why, one step back94 // 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 original95 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well96 if (!isProxyable(parent)) return97 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 98–102why, one step back98 each(parent, (i, child) => {99 if (isProxy(child)) {100 parent[i] = finalize(child)101 } else finalizeNonProxiedObject(child)102 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 103–112why, one step back103 // always freeze completely new data104 freeze(parent)105}106107export function verifyReturnValue(value) {108 // values either than undefined will trigger warning;109 if (value !== undefined)110 console.warn(111 `Immer callback expects no return value. However ${typeof value} was returned`112 )Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 113why, one step back113}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 114–122why, one step back114115export function is(x, y) {116 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js117 if (x === y) {118 return x !== 0 || 1 / x === 1 / y119 } else {120 return x !== x && y !== y121 }122}