common.js
This file
Path
src/common.js
175 lines · 5.1 KiB · JavaScript
Last changed by
Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–2why, one step back1import {generatePatches} from "./patches"2Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670
lines 3–6why, one step back3export const PROXY_STATE =4 typeof Symbol !== "undefined"5 ? Symbol("immer-proxy-state")6 : "__$immer_state"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 7why, one step back7Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 8–10why, one step back8export const RETURNED_AND_MODIFIED_ERROR =9 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."10Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
lines 11–14why, one step back11function verifyMinified() {}1213const inProduction =14 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002
line 15why, one step back15 verifyMinified.name !== "verifyMinified"Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909
line 16why, one step back16Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 17why, one step back17let autoFreeze = !inProductionMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 18–39why, one step back18let useProxies = typeof Proxy !== "undefined"1920/**21 * Automatically freezes any state trees generated by immer.22 * This protects against accidental modifications of the state tree outside of an immer function.23 * This comes with a performance impact, so it is recommended to disable this option in production.24 * It is by default enabled.25 *26 * @returns {void}27 */28export function setAutoFreeze(enableAutoFreeze) {29 autoFreeze = enableAutoFreeze30}3132export function setUseProxies(value) {33 useProxies = value34}3536export function getUseProxies() {37 return useProxies38}39Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 40–58why, one step back40export function isProxy(value) {41 return !!value && !!value[PROXY_STATE]42}4344export function isProxyable(value) {45 if (!value) return false46 if (typeof value !== "object") return false47 if (Array.isArray(value)) return true48 const proto = Object.getPrototypeOf(value)49 return proto === null || proto === Object.prototype50}5152export function freeze(value) {53 if (autoFreeze) {54 Object.freeze(value)55 }56 return value57}58Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 59–65why, one step back59export function original(value) {60 if (value && value[PROXY_STATE]) {61 return value[PROXY_STATE].base62 }63 // otherwise return undefined64}65Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 66–76why, one step back66const assign =67 Object.assign ||68 function assign(target, value) {69 for (let key in value) {70 if (has(value, key)) {71 target[key] = value[key]72 }73 }74 return target75 }76Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 77why, one step back77export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 78why, one step back78 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 79why, one step back79 const target = value.__proto__ === undefined ? Object.create(null) : {}Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 80why, one step back80 return assign(target, value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 81why, one step back81}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 82why, one step back82Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 83–90why, one step back83export function each(value, cb) {84 if (Array.isArray(value)) {85 for (let i = 0; i < value.length; i++) cb(i, value[i])86 } else {87 for (let key in value) cb(key, value[key])88 }89}90`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 91why, one step back91export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 92why, one step back92 return Object.prototype.hasOwnProperty.call(thing, prop)`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 93–94why, one step back93}94Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 95why, one step back95// given a base object, returns it if unmodified, or return the changed cloned if modifiedMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 96why, one step back96export function finalize(base, path, patches, inversePatches) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 97–101why, one step back97 if (isProxy(base)) {98 const state = base[PROXY_STATE]99 if (state.modified === true) {100 if (state.finalized === true) return state.copy101 state.finalized = trueMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 102why, one step back102 const result = finalizeObject(Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 103why, one step back103 useProxies ? state.copy : (state.copy = shallowCopy(base)),Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 104–107why, one step back104 state,105 path,106 patches,107 inversePatchesMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 108why, one step back108 )Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 109–117why, one step back109 generatePatches(110 state,111 path,112 patches,113 inversePatches,114 state.base,115 result116 )117 return resultMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 118–120why, one step back118 } else {119 return state.base120 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 121why, one step back121 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 122why, one step back122 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 123–125why, one step back123 return base124}125Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 126why, one step back126function finalizeObject(copy, state, path, patches, inversePatches) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 127–128why, one step back127 const base = state.base128 each(copy, (prop, value) => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 129–139why, one step back129 if (value !== base[prop]) {130 // if there was an assignment on this property, we don't need to generate131 // patches for the subtree132 const generatePatches = patches && !has(state.assigned, prop)133 copy[prop] = finalize(134 value,135 generatePatches && path.concat(prop),136 generatePatches && patches,137 inversePatches138 )139 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 140–143why, one step back140 })141 return freeze(copy)142}143Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 144why, one step back144function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 145–148why, one step back145 // 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 original146 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well147 if (!isProxyable(parent)) return148 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 149–153why, one step back149 each(parent, (i, child) => {150 if (isProxy(child)) {151 parent[i] = finalize(child)152 } else finalizeNonProxiedObject(child)153 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 154–157why, one step back154 // always freeze completely new data155 freeze(parent)156}157Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 158–165why, one step back158export function verifyReturnValue(returnedValue, proxy, isProxyModified) {159 if (returnedValue !== undefined && returnedValue !== proxy) {160 // something was returned, and it wasn't the proxy itself161 if (isProxyModified)162 throw new Error(163 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."164 )165 }Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 166why, one step back166}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 167–175why, one step back167168export function is(x, y) {169 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js170 if (x === y) {171 return x !== 0 || 1 / x === 1 / y172 } else {173 return x !== x && y !== y174 }175}