common.js
This file
Path
src/common.js
138 lines · 4.1 KiB · JavaScript
Last changed by
Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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 back5Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 6–8why, one step back6export const RETURNED_AND_MODIFIED_ERROR =7 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."8Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
lines 9–12why, one step back9function verifyMinified() {}1011const inProduction =12 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002
line 13why, one step back13 verifyMinified.name !== "verifyMinified"Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
line 14why, one step back14Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 15why, one step back15let autoFreeze = !inProductionMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 16–37why, one step back16let useProxies = typeof Proxy !== "undefined"1718/**19 * Automatically freezes any state trees generated by immer.20 * This protects against accidental modifications of the state tree outside of an immer function.21 * This comes with a performance impact, so it is recommended to disable this option in production.22 * It is by default enabled.23 *24 * @returns {void}25 */26export function setAutoFreeze(enableAutoFreeze) {27 autoFreeze = enableAutoFreeze28}2930export function setUseProxies(value) {31 useProxies = value32}3334export function getUseProxies() {35 return useProxies36}37Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 38–56why, one step back38export function isProxy(value) {39 return !!value && !!value[PROXY_STATE]40}4142export function isProxyable(value) {43 if (!value) return false44 if (typeof value !== "object") return false45 if (Array.isArray(value)) return true46 const proto = Object.getPrototypeOf(value)47 return proto === null || proto === Object.prototype48}4950export function freeze(value) {51 if (autoFreeze) {52 Object.freeze(value)53 }54 return value55}56Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 57why, one step back57export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 58why, one step back58 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
lines 59–65why, one step back59 const target = value.__proto__ === undefined ? Object.create(null) : {}60 for (let key in value) {61 if (has(value, key)) {62 target[key] = value[key]63 }64 }65 return targetMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 66why, one step back66}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 67why, one step back67Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 68–75why, one step back68export function each(value, cb) {69 if (Array.isArray(value)) {70 for (let i = 0; i < value.length; i++) cb(i, value[i])71 } else {72 for (let key in value) cb(key, value[key])73 }74}75`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 76why, one step back76export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 77why, one step back77 return Object.prototype.hasOwnProperty.call(thing, prop)`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 78–79why, one step back78}79Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 80–86why, one step back80// given a base object, returns it if unmodified, or return the changed cloned if modified81export function finalize(base) {82 if (isProxy(base)) {83 const state = base[PROXY_STATE]84 if (state.modified === true) {85 if (state.finalized === true) return state.copy86 state.finalized = trueMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 87–93why, one step back87 return finalizeObject(88 useProxies ? state.copy : (state.copy = shallowCopy(base)),89 state90 )91 } else {92 return state.base93 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 94why, one step back94 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 95why, one step back95 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 96–98why, one step back96 return base97}98Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 99–106why, one step back99function finalizeObject(copy, state) {100 const base = state.base101 each(copy, (prop, value) => {102 if (value !== base[prop]) copy[prop] = finalize(value)103 })104 return freeze(copy)105}106Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 107why, one step back107function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 108–111why, one step back108 // 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 original109 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well110 if (!isProxyable(parent)) return111 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 112–116why, one step back112 each(parent, (i, child) => {113 if (isProxy(child)) {114 parent[i] = finalize(child)115 } else finalizeNonProxiedObject(child)116 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 117–120why, one step back117 // always freeze completely new data118 freeze(parent)119}120Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 121–128why, one step back121export function verifyReturnValue(returnedValue, proxy, isProxyModified) {122 if (returnedValue !== undefined && returnedValue !== proxy) {123 // something was returned, and it wasn't the proxy itself124 if (isProxyModified)125 throw new Error(126 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."127 )128 }Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 129why, one step back129}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 130–138why, one step back130131export function is(x, y) {132 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js133 if (x === y) {134 return x !== 0 || 1 / x === 1 / y135 } else {136 return x !== x && y !== y137 }138}