common.js
This file
Path
src/common.js
197 lines · 5.7 KiB · JavaScript
Last changed by
More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
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}56Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 57–67why, one step back57const assign =58 Object.assign ||59 function assign(target, value) {60 for (let key in value) {61 if (has(value, key)) {62 target[key] = value[key]63 }64 }65 return target66 }67Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 68why, one step back68export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 69why, one step back69 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 70why, one step back70 const target = value.__proto__ === undefined ? Object.create(null) : {}Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 71why, one step back71 return assign(target, value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 72why, one step back72}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 73why, one step back73Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 74–81why, one step back74export function each(value, cb) {75 if (Array.isArray(value)) {76 for (let i = 0; i < value.length; i++) cb(i, value[i])77 } else {78 for (let key in value) cb(key, value[key])79 }80}81`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 82why, one step back82export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 83why, one step back83 return Object.prototype.hasOwnProperty.call(thing, prop)`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 84–85why, one step back84}85Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 86why, one step back86// given a base object, returns it if unmodified, or return the changed cloned if modifiedMore tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 87why, one step back87export function finalize(base, path, patches, inversePatches) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 88–92why, one step back88 if (isProxy(base)) {89 const state = base[PROXY_STATE]90 if (state.modified === true) {91 if (state.finalized === true) return state.copy92 state.finalized = trueInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 93why, one step back93 const result = finalizeObject(Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 94why, one step back94 useProxies ? state.copy : (state.copy = shallowCopy(base)),Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 95–96why, one step back95 state,96 path,More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 97–98why, one step back97 patches,98 inversePatchesMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 99why, one step back99 )More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 100–107why, one step back100 generatePatches(101 state,102 path,103 patches,104 inversePatches,105 state.base,106 result107 )Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 108why, one step back108 return resultMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 109–111why, one step back109 } else {110 return state.base111 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 112why, one step back112 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 113why, one step back113 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 114–116why, one step back114 return base115}116More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 117–124why, one step back117function generatePatches(118 state,119 basepath,120 patches,121 inversePatches,122 baseValue,123 resultValue124) {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 125why, one step back125 if (patches)More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 126–127why, one step back126 each(state.assigned, (key, added) => {127 const path = basepath.concat(key)Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 128why, one step back128 patches.push(More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 129why, one step back129 addedInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 130–131why, one step back130 ? {131 op: "replace",More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 132–133why, one step back132 path,133 value: resultValue[key]Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 134–136why, one step back134 }135 : {136 op: "remove",More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 137why, one step back137 pathInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 138–139why, one step back138 }139 )More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 140–144why, one step back140 inversePatches.push(141 added && !key in baseValue142 ? {op: "remove", path}143 : {op: "replace", path, value: baseValue[key]}144 )Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 145–147why, one step back145 })146}147More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 148why, one step back148function finalizeObject(copy, state, path, patches, inversePatches) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 149–150why, one step back149 const base = state.base150 each(copy, (prop, value) => {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 151–157why, one step back151 if (value !== base[prop]) {152 // if there was an assignment on this property, we don't need to generate153 // patches for the subtree154 const generatePatches = patches && !has(state.assigned, prop)155 copy[prop] = finalize(156 value,157 generatePatches && path.concat(prop),More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 158–159why, one step back158 generatePatches && patches,159 inversePatchesInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 160–161why, one step back160 )161 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 162–165why, one step back162 })163 return freeze(copy)164}165Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 166why, one step back166function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 167–170why, one step back167 // 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 original168 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well169 if (!isProxyable(parent)) return170 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 171–175why, one step back171 each(parent, (i, child) => {172 if (isProxy(child)) {173 parent[i] = finalize(child)174 } else finalizeNonProxiedObject(child)175 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 176–179why, one step back176 // always freeze completely new data177 freeze(parent)178}179Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 180–187why, one step back180export function verifyReturnValue(returnedValue, proxy, isProxyModified) {181 if (returnedValue !== undefined && returnedValue !== proxy) {182 // something was returned, and it wasn't the proxy itself183 if (isProxyModified)184 throw new Error(185 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."186 )187 }Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 188why, one step back188}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 189–197why, one step back189190export function is(x, y) {191 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js192 if (x === y) {193 return x !== 0 || 1 / x === 1 / y194 } else {195 return x !== x && y !== y196 }197}