Skip to content
Myr

common.js

This file

Path

src/common.js

168 lines · 4.8 KiB · JavaScript

Last changed by

fix: never auto-freeze new objectsaleclarsonbefore adoption2f381f5

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 3–7why, one step back
3export const NOTHING =
4 typeof Symbol !== "undefined"5 ? Symbol("immer-nothing")6 : {["immer-nothing"]: true}7

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

lines 8–11why, one step back
8export 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 back
12

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

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

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

lines 16–19why, one step back
16function 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 back
20 verifyMinified.name !== "verifyMinified"

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

line 21why, one step back
21

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

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 23why, one step back
23let useProxies = typeof Proxy !== "undefined" && typeof Reflect !== "undefined"

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

lines 24–44why, one step back
24
25/**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}44

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

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

Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2

lines 64–70why, one step back
64export function original(value) {
65 if (value && value[PROXY_STATE]) {66 return value[PROXY_STATE].base67 }68 // otherwise return undefined69}70

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 71–81why, one step back
71const 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 }81

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

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

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

line 83why, one step back
83 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 back
84 const target = value.__proto__ === undefined ? Object.create(null) : {}

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

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

line 86why, one step back
86}

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

line 87why, one step back
87

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

lines 88–95why, one step back
88export 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 back
96export function has(thing, prop) {

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

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

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

lines 98–99why, one step back
98}
99

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

lines 102–106why, one step back
102 if (isProxy(base)) {
103 const state = base[PROXY_STATE]104 if (state.modified === true) {105 if (state.finalized === true) return state.copy106 state.finalized = true

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 109–112why, one step back
109 state,
110 path,111 patches,112 inversePatches

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

line 113why, one step back
113 )

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 114–122why, one step back
114 generatePatches(
115 state,116 path,117 patches,118 inversePatches,119 state.base,120 result121 )122 return result

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

lines 123–125why, one step back
123 } else {
124 return state.base125 }

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

line 126why, one step back
126 }

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

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

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

lines 128–130why, one step back
128 return base
129}130

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 134–144why, one step back
134 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 back
145 })
146 return freeze(copy)147}148

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

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

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

lines 150–153why, one step back
150 // 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
151 // 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)) return

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

lines 154–158why, one step back
154 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–160why, one step back
159}
160

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

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