common.js
This file
Path
src/common.js
180 lines · 5.3 KiB · JavaScript
Last changed by
fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–2why, one step back1import {generatePatches} from "./patches"2Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 3–7why, one step back3export const NOTHING =4 typeof Symbol !== "undefined"5 ? Symbol("immer-nothing")6 : {["immer-nothing"]: true}7Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670
lines 8–11why, one step back8export const PROXY_STATE =9 typeof Symbol !== "undefined"10 ? Symbol("immer-proxy-state")11 : "__$immer_state"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 12why, one step back12Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 13–15why, one step back13export const RETURNED_AND_MODIFIED_ERROR =14 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."15Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
lines 16–19why, one step back16function verifyMinified() {}1718const inProduction =19 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002
line 20why, one step back20 verifyMinified.name !== "verifyMinified"Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
line 21why, one step back21Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 22why, one step back22let autoFreeze = !inProductionfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 23why, one step back23let useProxies = typeof Proxy !== "undefined" && typeof Reflect !== "undefined"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 24–44why, one step back2425/**26 * Automatically freezes any state trees generated by immer.27 * This protects against accidental modifications of the state tree outside of an immer function.28 * This comes with a performance impact, so it is recommended to disable this option in production.29 * It is by default enabled.30 *31 * @returns {void}32 */33export function setAutoFreeze(enableAutoFreeze) {34 autoFreeze = enableAutoFreeze35}3637export function setUseProxies(value) {38 useProxies = value39}4041export function getUseProxies() {42 return useProxies43}44Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 45–63why, one step back45export function isProxy(value) {46 return !!value && !!value[PROXY_STATE]47}4849export function isProxyable(value) {50 if (!value) return false51 if (typeof value !== "object") return false52 if (Array.isArray(value)) return true53 const proto = Object.getPrototypeOf(value)54 return proto === null || proto === Object.prototype55}5657export function freeze(value) {58 if (autoFreeze) {59 Object.freeze(value)60 }61 return value62}63Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 64–70why, one step back64export function original(value) {65 if (value && value[PROXY_STATE]) {66 return value[PROXY_STATE].base67 }68 // otherwise return undefined69}70Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 71–81why, one step back71const assign =72 Object.assign ||73 function assign(target, value) {74 for (let key in value) {75 if (has(value, key)) {76 target[key] = value[key]77 }78 }79 return target80 }81Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 82why, one step back82export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 83why, one step back83 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 84why, one step back84 const target = value.__proto__ === undefined ? Object.create(null) : {}Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 85why, one step back85 return assign(target, value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 86why, one step back86}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 87why, one step back87Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 88–95why, one step back88export function each(value, cb) {89 if (Array.isArray(value)) {90 for (let i = 0; i < value.length; i++) cb(i, value[i])91 } else {92 for (let key in value) cb(key, value[key])93 }94}95`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 96why, one step back96export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 97why, one step back97 return Object.prototype.hasOwnProperty.call(thing, prop)`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 98–99why, one step back98}99Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 100why, one step back100// given a base object, returns it if unmodified, or return the changed cloned if modifiedMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 101why, one step back101export function finalize(base, path, patches, inversePatches) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 102–106why, one step back102 if (isProxy(base)) {103 const state = base[PROXY_STATE]104 if (state.modified === true) {105 if (state.finalized === true) return state.copy106 state.finalized = trueMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 107why, one step back107 const result = finalizeObject(Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 108why, one step back108 useProxies ? state.copy : (state.copy = shallowCopy(base)),Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 109–112why, one step back109 state,110 path,111 patches,112 inversePatchesMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 113why, one step back113 )Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 114–122why, one step back114 generatePatches(115 state,116 path,117 patches,118 inversePatches,119 state.base,120 result121 )122 return resultMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 123–125why, one step back123 } else {124 return state.base125 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 126why, one step back126 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 127why, one step back127 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 128–130why, one step back128 return base129}130Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 131why, one step back131function finalizeObject(copy, state, path, patches, inversePatches) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 132–133why, one step back132 const base = state.base133 each(copy, (prop, value) => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 134–144why, one step back134 if (value !== base[prop]) {135 // if there was an assignment on this property, we don't need to generate136 // patches for the subtree137 const generatePatches = patches && !has(state.assigned, prop)138 copy[prop] = finalize(139 value,140 generatePatches && path.concat(prop),141 generatePatches && patches,142 inversePatches143 )144 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 145–148why, one step back145 })146 return freeze(copy)147}148Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 149why, one step back149function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 150–153why, one step back150 // 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 original151 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well152 if (!isProxyable(parent)) return153 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 154–158why, one step back154 each(parent, (i, child) => {155 if (isProxy(child)) {156 parent[i] = finalize(child)157 } else finalizeNonProxiedObject(child)158 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 159–162why, one step back159 // always freeze completely new data160 freeze(parent)161}162Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 163–170why, one step back163export function verifyReturnValue(returnedValue, proxy, isProxyModified) {164 if (returnedValue !== undefined && returnedValue !== proxy) {165 // something was returned, and it wasn't the proxy itself166 if (isProxyModified)167 throw new Error(168 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."169 )170 }Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 171why, one step back171}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 172–180why, one step back172173export function is(x, y) {174 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js175 if (x === y) {176 return x !== 0 || 1 / x === 1 / y177 } else {178 return x !== x && y !== y179 }180}