common.js
This file
Path
src/common.js
129 lines · 3.9 KiB · JavaScript
Last changed by
Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
History of this fileTake it
Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 1–8why, one step back1import {2 finalizeArray as finalizeArrayProxy,3 finalizeObject as finalizeObjectProxy4} from "./proxy"5import {6 finalizeArray as finalizeArrayEs5,7 finalizeObject as finalizeObjectEs58} from "./es5"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 9why, one step back9Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670
lines 10–13why, one step back10export const PROXY_STATE =11 typeof Symbol !== "undefined"12 ? Symbol("immer-proxy-state")13 : "__$immer_state"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 14–37why, one step back1415let autoFreeze = true16let 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}37Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 38–56why, one step back38export 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}56Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 57–58why, one step back57export function shallowCopy(value) {58 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 59why, one step back59}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 60why, one step back60Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 61–90why, one step back61export 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}6869// given a base object, returns it if unmodified, or return the changed cloned if modified70export function finalize(base) {71 if (isProxy(base)) {72 const state = base[PROXY_STATE]73 if (state.modified === true) {74 if (state.finalized === true) return state.copy75 state.finalized = true76 if (Array.isArray(state.base))77 return useProxies78 ? finalizeArrayProxy(state)79 : finalizeArrayEs5(base, state)80 return useProxies81 ? finalizeObjectProxy(state)82 : finalizeObjectEs5(base, state)83 } else return state.base84 } else if (base !== null && typeof base === "object") {85 finalizeNonProxiedObject(base)86 }87 return base88}8990function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 91–98why, one step back91 // 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 original92 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well93 if (!isProxyable(parent)) return94 if (Object.isFrozen(parent)) return95 if (Array.isArray(parent)) {96 for (let i = 0; i < parent.length; i++) {97 const child = parent[i]98 if (isProxy(child)) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 99–100why, one step back99 parent[i] = finalize(child)100 } else finalizeNonProxiedObject(child)Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 101–105why, one step back101 }102 } else {103 for (let key in parent) {104 const child = parent[key]105 if (isProxy(child)) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 106–107why, one step back106 parent[key] = finalize(child)107 } else finalizeNonProxiedObject(child)Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 108–109why, one step back108 }109 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 110–119why, one step back110 // always freeze completely new data111 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 back120}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
lines 121–129why, one step back121122export 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}