Skip to content
Myr

common.js

This file

Path

src/common.js

172 lines · 5.1 KiB · JavaScript

Last changed by

Add original functionRichieAHBbefore adoption590025f

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1–2why, one step back
1import {generatePatches} from "./patches"
2

Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670

lines 3–6why, one step back
3export 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 back
7

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 8–10why, one step back
8export 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."10

Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909

lines 11–14why, one step back
11function 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 back
15 verifyMinified.name !== "verifyMinified"

Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909

line 16why, one step back
16

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 17why, one step back
17let autoFreeze = !inProduction

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 18–39why, one step back
18let 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}39

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 40–58why, one step back
40export 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}58

Add original functionRichieAHBbefore adoption590025f

lines 59–62why, one step back
59export function original(value) {
60 return value && value[PROXY_STATE] ? value[PROXY_STATE].base : value61}62

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 63–73why, one step back
63const assign =
64 Object.assign ||65 function assign(target, value) {66 for (let key in value) {67 if (has(value, key)) {68 target[key] = value[key]69 }70 }71 return target72 }73

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 74why, one step back
74export function shallowCopy(value) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 75why, one step back
75 if (Array.isArray(value)) return value.slice()

Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e

line 76why, one step back
76 const target = value.__proto__ === undefined ? Object.create(null) : {}

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 77why, one step back
77 return assign(target, value)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 78why, one step back
78}

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 79why, one step back
79

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 80–87why, one step back
80export function each(value, cb) {
81 if (Array.isArray(value)) {82 for (let i = 0; i < value.length; i++) cb(i, value[i])83 } else {84 for (let key in value) cb(key, value[key])85 }86}87

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 88why, one step back
88export function has(thing, prop) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 89why, one step back
89 return Object.prototype.hasOwnProperty.call(thing, prop)

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

lines 90–91why, one step back
90}
91

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 92why, one step back
92// given a base object, returns it if unmodified, or return the changed cloned if modified

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 93why, one step back
93export function finalize(base, path, patches, inversePatches) {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 94–98why, one step back
94 if (isProxy(base)) {
95 const state = base[PROXY_STATE]96 if (state.modified === true) {97 if (state.finalized === true) return state.copy98 state.finalized = true

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 99why, one step back
99 const result = finalizeObject(

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 100why, one step back
100 useProxies ? state.copy : (state.copy = shallowCopy(base)),

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 101–104why, one step back
101 state,
102 path,103 patches,104 inversePatches

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 105why, one step back
105 )

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 106–114why, one step back
106 generatePatches(
107 state,108 path,109 patches,110 inversePatches,111 state.base,112 result113 )114 return result

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 115–117why, one step back
115 } else {
116 return state.base117 }

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 118why, one step back
118 }

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 119why, one step back
119 finalizeNonProxiedObject(base)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 120–122why, one step back
120 return base
121}122

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 123why, one step back
123function finalizeObject(copy, state, path, patches, inversePatches) {

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 124–125why, one step back
124 const base = state.base
125 each(copy, (prop, value) => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 126–136why, one step back
126 if (value !== base[prop]) {
127 // if there was an assignment on this property, we don't need to generate128 // patches for the subtree129 const generatePatches = patches && !has(state.assigned, prop)130 copy[prop] = finalize(131 value,132 generatePatches && path.concat(prop),133 generatePatches && patches,134 inversePatches135 )136 }

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 137–140why, one step back
137 })
138 return freeze(copy)139}140

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 141why, one step back
141function finalizeNonProxiedObject(parent) {

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 142–145why, one step back
142 // 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 original
143 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well144 if (!isProxyable(parent)) return145 if (Object.isFrozen(parent)) return

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 146–150why, one step back
146 each(parent, (i, child) => {
147 if (isProxy(child)) {148 parent[i] = finalize(child)149 } else finalizeNonProxiedObject(child)150 })

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 151–154why, one step back
151 // always freeze completely new data
152 freeze(parent)153}154

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 155–162why, one step back
155export function verifyReturnValue(returnedValue, proxy, isProxyModified) {
156 if (returnedValue !== undefined && returnedValue !== proxy) {157 // something was returned, and it wasn't the proxy itself158 if (isProxyModified)159 throw new Error(160 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."161 )162 }

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 163why, one step back
163}

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

lines 164–172why, one step back
164
165export function is(x, y) {166 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js167 if (x === y) {168 return x !== 0 || 1 / x === 1 / y169 } else {170 return x !== x && y !== y171 }172}