common.js
This file
Path
src/common.js
126 lines · 3.6 KiB · JavaScript
Last changed by
`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
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–65why, 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}65`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 66–69why, one step back66export function has(thing, prop) {67 return Object.hasOwnProperty(thing, prop)68}69Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 70–76why, one step back70// given a base object, returns it if unmodified, or return the changed cloned if modified71export function finalize(base) {72 if (isProxy(base)) {73 const state = base[PROXY_STATE]74 if (state.modified === true) {75 if (state.finalized === true) return state.copy76 state.finalized = trueMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 77–83why, one step back77 return finalizeObject(78 useProxies ? state.copy : (state.copy = shallowCopy(base)),79 state80 )81 } else {82 return state.base83 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 84why, one step back84 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 85why, one step back85 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 86–88why, one step back86 return base87}88Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 89–96why, one step back89function finalizeObject(copy, state) {90 const base = state.base91 each(copy, (prop, value) => {92 if (value !== base[prop]) copy[prop] = finalize(value)93 })94 return freeze(copy)95}96Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 97why, one step back97function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 98–101why, one step back98 // 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 original99 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well100 if (!isProxyable(parent)) return101 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 102–106why, one step back102 each(parent, (i, child) => {103 if (isProxy(child)) {104 parent[i] = finalize(child)105 } else finalizeNonProxiedObject(child)106 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 107–116why, one step back107 // always freeze completely new data108 freeze(parent)109}110111export function verifyReturnValue(value) {112 // values either than undefined will trigger warning;113 if (value !== undefined)114 console.warn(115 `Immer callback expects no return value. However ${typeof value} was returned`116 )Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 117why, one step back117}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 118–126why, one step back118119export function is(x, y) {120 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js121 if (x === y) {122 return x !== 0 || 1 / x === 1 / y123 } else {124 return x !== x && y !== y125 }126}