common.js
This file
Path
src/common.js
168 lines · 5.0 KiB · JavaScript
Last changed by
Moved to single file setupMichel Weststratebefore adoption160038b
History of this fileTake it
Moved to single file setupMichel Weststratebefore adoption160038b
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}58Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 59–69why, one step back59const assign =60 Object.assign ||61 function assign(target, value) {62 for (let key in value) {63 if (has(value, key)) {64 target[key] = value[key]65 }66 }67 return target68 }69Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 70why, one step back70export function shallowCopy(value) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 71why, one step back71 if (Array.isArray(value)) return value.slice()Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 72why, one step back72 const target = value.__proto__ === undefined ? Object.create(null) : {}Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 73why, one step back73 return assign(target, value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 74why, one step back74}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 75why, one step back75Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 76–83why, one step back76export function each(value, cb) {77 if (Array.isArray(value)) {78 for (let i = 0; i < value.length; i++) cb(i, value[i])79 } else {80 for (let key in value) cb(key, value[key])81 }82}83`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 84why, one step back84export function has(thing, prop) {Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
line 85why, one step back85 return Object.prototype.hasOwnProperty.call(thing, prop)`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
lines 86–87why, one step back86}87Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 88why, one step back88// given a base object, returns it if unmodified, or return the changed cloned if modifiedMore tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 89why, one step back89export function finalize(base, path, patches, inversePatches) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 90–94why, one step back90 if (isProxy(base)) {91 const state = base[PROXY_STATE]92 if (state.modified === true) {93 if (state.finalized === true) return state.copy94 state.finalized = trueInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 95why, one step back95 const result = finalizeObject(Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 96why, one step back96 useProxies ? state.copy : (state.copy = shallowCopy(base)),Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 97–98why, one step back97 state,98 path,More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 99–100why, one step back99 patches,100 inversePatchesMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 101why, one step back101 )More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 102–109why, one step back102 generatePatches(103 state,104 path,105 patches,106 inversePatches,107 state.base,108 result109 )Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 110why, one step back110 return resultMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 111–113why, one step back111 } else {112 return state.base113 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 114why, one step back114 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
line 115why, one step back115 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 116why, one step back116 return baseInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 117–118why, one step back117}118More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 119why, one step back119function finalizeObject(copy, state, path, patches, inversePatches) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 120–121why, one step back120 const base = state.base121 each(copy, (prop, value) => {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 122–128why, one step back122 if (value !== base[prop]) {123 // if there was an assignment on this property, we don't need to generate124 // patches for the subtree125 const generatePatches = patches && !has(state.assigned, prop)126 copy[prop] = finalize(127 value,128 generatePatches && path.concat(prop),More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 129–130why, one step back129 generatePatches && patches,130 inversePatchesInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 131–132why, one step back131 )132 }Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 133–136why, one step back133 })134 return freeze(copy)135}136Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 137why, one step back137function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 138–141why, one step back138 // 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 original139 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well140 if (!isProxyable(parent)) return141 if (Object.isFrozen(parent)) returnMerge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 142–146why, one step back142 each(parent, (i, child) => {143 if (isProxy(child)) {144 parent[i] = finalize(child)145 } else finalizeNonProxiedObject(child)146 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 147–150why, one step back147 // always freeze completely new data148 freeze(parent)149}150Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 151–158why, one step back151export function verifyReturnValue(returnedValue, proxy, isProxyModified) {152 if (returnedValue !== undefined && returnedValue !== proxy) {153 // something was returned, and it wasn't the proxy itself154 if (isProxyModified)155 throw new Error(156 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."157 )158 }Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 159why, one step back159}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
lines 160–168why, one step back160161export function is(x, y) {162 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js163 if (x === y) {164 return x !== 0 || 1 / x === 1 / y165 } else {166 return x !== x && y !== y167 }168}