Skip to content
Myr

common.js

This file

Path

src/common.js

134 lines · 4.0 KiB · JavaScript

Last changed by

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

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

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

lines 6–8why, one step back
6export const RETURNED_AND_MODIFIED_ERROR =
7 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."8

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

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

Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002

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

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

line 14why, one step back
14

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

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

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

lines 16–37why, one step back
16let useProxies = typeof Proxy !== "undefined"
1718/**19 * Automatically freezes any state trees generated by immer.20 * This protects against accidental modifications of the state tree outside of an immer function.21 * This comes with a performance impact, so it is recommended to disable this option in production.22 * It is by default enabled.23 *24 * @returns {void}25 */26export function setAutoFreeze(enableAutoFreeze) {27 autoFreeze = enableAutoFreeze28}2930export function setUseProxies(value) {31 useProxies = value32}3334export function getUseProxies() {35 return useProxies36}37

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

lines 38–56why, one step back
38export function isProxy(value) {
39 return !!value && !!value[PROXY_STATE]40}4142export function isProxyable(value) {43 if (!value) return false44 if (typeof value !== "object") return false45 if (Array.isArray(value)) return true46 const proto = Object.getPrototypeOf(value)47 return proto === null || proto === Object.prototype48}4950export function freeze(value) {51 if (autoFreeze) {52 Object.freeze(value)53 }54 return value55}56

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

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

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

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

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

line 62why, one step back
62}

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

line 63why, one step back
63

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

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

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

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

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

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

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

lines 74–75why, one step back
74}
75

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

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

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

lines 83–89why, one step back
83 return finalizeObject(
84 useProxies ? state.copy : (state.copy = shallowCopy(base)),85 state86 )87 } else {88 return state.base89 }

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

line 90why, one step back
90 }

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

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

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

lines 92–94why, one step back
92 return base
93}94

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

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

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

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

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

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

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

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

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

lines 113–116why, one step back
113 // always freeze completely new data
114 freeze(parent)115}116

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

lines 117–124why, one step back
117export function verifyReturnValue(returnedValue, proxy, isProxyModified) {
118 if (returnedValue !== undefined && returnedValue !== proxy) {119 // something was returned, and it wasn't the proxy itself120 if (isProxyModified)121 throw new Error(122 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."123 )124 }

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

line 125why, one step back
125}

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

lines 126–134why, one step back
126
127export function is(x, y) {128 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js129 if (x === y) {130 return x !== 0 || 1 / x === 1 / y131 } else {132 return x !== x && y !== y133 }134}