Skip to content
Myr

common.js

This file

Path

src/common.js

126 lines · 3.6 KiB · JavaScript

Last changed by

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

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–12why, one step back
6function verifyMinified() {}
78const inProduction =9 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||10 verifyMinified.name === "verifyMinified"11let autoFreeze = !inProduction12

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

lines 54–55why, one step back
54export function shallowCopy(value) {
55 return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?

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

line 56why, one step back
56}

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

line 57why, one step back
57

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

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

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

lines 66–69why, one step back
66export function has(thing, prop) {
67 return Object.hasOwnProperty(thing, prop)68}69

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

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

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

lines 77–83why, one step back
77 return finalizeObject(
78 useProxies ? state.copy : (state.copy = shallowCopy(base)),79 state80 )81 } else {82 return state.base83 }

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

line 84why, one step back
84 }

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

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

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

lines 86–88why, one step back
86 return base
87}88

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

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

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

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

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

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

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

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

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

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

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

line 117why, one step back
117}

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

lines 118–126why, one step back
118
119export function is(x, y) {120 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js121 if (x === y) {122 return x !== 0 || 1 / x === 1 / y123 } else {124 return x !== x && y !== y125 }126}