common.js
This file
Path
src/common.js
110 lines · 3.2 KiB · JavaScript
Last changed by
Removed some duplicate codeMichel Weststratebefore adoptionee544cc
History of this fileTake it
Removed some duplicate codeMichel Weststratebefore adoptionee544cc
lines 1–2why, one step back1import {finalizeObject as finalizeObjectProxy} from "./proxy"2import {finalizeObject as finalizeObjectEs5} from "./es5"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 3why, one step back3Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670
lines 4–7why, one step back4export const PROXY_STATE =5 typeof Symbol !== "undefined"6 ? Symbol("immer-proxy-state")7 : "__$immer_state"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 8–31why, one step back89let autoFreeze = true10let useProxies = typeof Proxy !== "undefined"1112/**13 * Automatically freezes any state trees generated by immer.14 * This protects against accidental modifications of the state tree outside of an immer function.15 * This comes with a performance impact, so it is recommended to disable this option in production.16 * It is by default enabled.17 *18 * @returns {void}19 */20export function setAutoFreeze(enableAutoFreeze) {21 autoFreeze = enableAutoFreeze22}2324export function setUseProxies(value) {25 useProxies = value26}2728export function getUseProxies() {29 return useProxies30}31Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 32–50why, one step back32export function isProxy(value) {33 return !!value && !!value[PROXY_STATE]34}3536export function isProxyable(value) {37 if (!value) return false38 if (typeof value !== "object") return false39 if (Array.isArray(value)) return true40 const proto = Object.getPrototypeOf(value)41 return proto === null || proto === Object.prototype42}4344export function freeze(value) {45 if (autoFreeze) {46 Object.freeze(value)47 }48 return value49}50Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 51–52why, one step back51export function shallowCopy(value) {52 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 53why, one step back53}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 54why, one step back54Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 55–72why, one step back55export function each(value, cb) {56 if (Array.isArray(value)) {57 for (let i = 0; i < value.length; i++) cb(i, value[i])58 } else {59 for (let key in value) cb(key, value[key])60 }61}6263// given a base object, returns it if unmodified, or return the changed cloned if modified64export function finalize(base) {65 if (isProxy(base)) {66 const state = base[PROXY_STATE]67 if (state.modified === true) {68 if (state.finalized === true) return state.copy69 state.finalized = true70 return useProxies71 ? finalizeObjectProxy(state)72 : finalizeObjectEs5(base, state)Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
lines 73–75why, one step back73 } else {74 return state.base75 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 76why, one step back76 }Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
line 77why, one step back77 finalizeNonProxiedObject(base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 78–80why, one step back78 return base79}80Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
line 81why, one step back81export function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 82–85why, one step back82 // 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 original83 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well84 if (!isProxyable(parent)) return85 if (Object.isFrozen(parent)) returnFixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
lines 86–90why, one step back86 each(parent, (i, child) => {87 if (isProxy(child)) {88 parent[i] = finalize(child)89 } else finalizeNonProxiedObject(child)90 })Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 91–100why, one step back91 // always freeze completely new data92 freeze(parent)93}9495export function verifyReturnValue(value) {96 // values either than undefined will trigger warning;97 if (value !== undefined)98 console.warn(99 `Immer callback expects no return value. However ${typeof value} was returned`100 )Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 101why, one step back101}Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
lines 102–110why, one step back102103export function is(x, y) {104 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js105 if (x === y) {106 return x !== 0 || 1 / x === 1 / y107 } else {108 return x !== x && y !== y109 }110}