Skip to content
Myr

common.js

This file

Path

src/common.js

116 lines · 3.3 KiB · JavaScript

Last changed by

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

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

lines 5–28why, one step back
5
6let autoFreeze = true7let useProxies = typeof Proxy !== "undefined"89/**10 * Automatically freezes any state trees generated by immer.11 * This protects against accidental modifications of the state tree outside of an immer function.12 * This comes with a performance impact, so it is recommended to disable this option in production.13 * It is by default enabled.14 *15 * @returns {void}16 */17export function setAutoFreeze(enableAutoFreeze) {18 autoFreeze = enableAutoFreeze19}2021export function setUseProxies(value) {22 useProxies = value23}2425export function getUseProxies() {26 return useProxies27}28

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

lines 29–47why, one step back
29export function isProxy(value) {
30 return !!value && !!value[PROXY_STATE]31}3233export function isProxyable(value) {34 if (!value) return false35 if (typeof value !== "object") return false36 if (Array.isArray(value)) return true37 const proto = Object.getPrototypeOf(value)38 return proto === null || proto === Object.prototype39}4041export function freeze(value) {42 if (autoFreeze) {43 Object.freeze(value)44 }45 return value46}47

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

lines 48–49why, one step back
48export function shallowCopy(value) {
49 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 50why, one step back
50}

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

line 51why, one step back
51

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

lines 52–66why, one step back
52export function each(value, cb) {
53 if (Array.isArray(value)) {54 for (let i = 0; i < value.length; i++) cb(i, value[i])55 } else {56 for (let key in value) cb(key, value[key])57 }58}5960// given a base object, returns it if unmodified, or return the changed cloned if modified61export function finalize(base) {62 if (isProxy(base)) {63 const state = base[PROXY_STATE]64 if (state.modified === true) {65 if (state.finalized === true) return state.copy66 state.finalized = true

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

lines 67–73why, one step back
67 return finalizeObject(
68 useProxies ? state.copy : (state.copy = shallowCopy(base)),69 state70 )71 } else {72 return state.base73 }

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

line 74why, one step back
74 }

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

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

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

lines 76–78why, one step back
76 return base
77}78

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

lines 79–86why, one step back
79function finalizeObject(copy, state) {
80 const base = state.base81 each(copy, (prop, value) => {82 if (value !== base[prop]) copy[prop] = finalize(value)83 })84 return freeze(copy)85}86

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

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

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

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

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

lines 92–96why, one step back
92 each(parent, (i, child) => {
93 if (isProxy(child)) {94 parent[i] = finalize(child)95 } else finalizeNonProxiedObject(child)96 })

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

lines 97–106why, one step back
97 // always freeze completely new data
98 freeze(parent)99}100101export function verifyReturnValue(value) {102 // values either than undefined will trigger warning;103 if (value !== undefined)104 console.warn(105 `Immer callback expects no return value. However ${typeof value} was returned`106 )

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

line 107why, one step back
107}

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

lines 108–116why, one step back
108
109export function is(x, y) {110 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js111 if (x === y) {112 return x !== 0 || 1 / x === 1 / y113 } else {114 return x !== x && y !== y115 }116}