Skip to content
Myr

common.js

This file

Path

src/common.js

129 lines · 3.7 KiB · JavaScript

Last changed by

Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002

History of this file

Take it

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

lines 1–4why, one step back
1export const PROXY_STATE =
2 typeof Symbol !== "undefined"3 ? Symbol("immer-proxy-state")4 : "__$immer_state"

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

line 5why, one step back
5

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

lines 6–9why, one step back
6function verifyMinified() {}
78const inProduction =9 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||

Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002

line 10why, one step back
10 verifyMinified.name !== "verifyMinified"

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

lines 11–12why, one step back
11let autoFreeze = !inProduction
12

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

lines 13–34why, one step back
13let useProxies = typeof Proxy !== "undefined"
1415/**16 * Automatically freezes any state trees generated by immer.17 * This protects against accidental modifications of the state tree outside of an immer function.18 * This comes with a performance impact, so it is recommended to disable this option in production.19 * It is by default enabled.20 *21 * @returns {void}22 */23export function setAutoFreeze(enableAutoFreeze) {24 autoFreeze = enableAutoFreeze25}2627export function setUseProxies(value) {28 useProxies = value29}3031export function getUseProxies() {32 return useProxies33}34

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

lines 35–53why, one step back
35export function isProxy(value) {
36 return !!value && !!value[PROXY_STATE]37}3839export function isProxyable(value) {40 if (!value) return false41 if (typeof value !== "object") return false42 if (Array.isArray(value)) return true43 const proto = Object.getPrototypeOf(value)44 return proto === null || proto === Object.prototype45}4647export function freeze(value) {48 if (autoFreeze) {49 Object.freeze(value)50 }51 return value52}53

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

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

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

lines 55–58why, one step back
55 if (Array.isArray(value)) return value.slice()
56 if (value.__proto__ === undefined)57 return Object.assign(Object.create(null), value)58 return Object.assign({}, value)

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

line 59why, one step back
59}

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

line 60why, one step back
60

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

lines 61–68why, one step back
61export function each(value, cb) {
62 if (Array.isArray(value)) {63 for (let i = 0; i < value.length; i++) cb(i, value[i])64 } else {65 for (let key in value) cb(key, value[key])66 }67}68

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

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

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

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

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

lines 71–72why, one step back
71}
72

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

lines 73–79why, one step back
73// given a base object, returns it if unmodified, or return the changed cloned if modified
74export function finalize(base) {75 if (isProxy(base)) {76 const state = base[PROXY_STATE]77 if (state.modified === true) {78 if (state.finalized === true) return state.copy79 state.finalized = true

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

lines 80–86why, one step back
80 return finalizeObject(
81 useProxies ? state.copy : (state.copy = shallowCopy(base)),82 state83 )84 } else {85 return state.base86 }

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

line 87why, one step back
87 }

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

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

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

lines 89–91why, one step back
89 return base
90}91

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

lines 92–99why, one step back
92function finalizeObject(copy, state) {
93 const base = state.base94 each(copy, (prop, value) => {95 if (value !== base[prop]) copy[prop] = finalize(value)96 })97 return freeze(copy)98}99

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

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

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

lines 101–104why, one step back
101 // 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
102 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well103 if (!isProxyable(parent)) return104 if (Object.isFrozen(parent)) return

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

lines 105–109why, one step back
105 each(parent, (i, child) => {
106 if (isProxy(child)) {107 parent[i] = finalize(child)108 } else finalizeNonProxiedObject(child)109 })

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

lines 110–119why, one step back
110 // always freeze completely new data
111 freeze(parent)112}113114export function verifyReturnValue(value) {115 // values either than undefined will trigger warning;116 if (value !== undefined)117 console.warn(118 `Immer callback expects no return value. However ${typeof value} was returned`119 )

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

line 120why, one step back
120}

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

lines 121–129why, one step back
121
122export function is(x, y) {123 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js124 if (x === y) {125 return x !== 0 || 1 / x === 1 / y126 } else {127 return x !== x && y !== y128 }129}