common.js
This file
Path
src/common.js
117 lines · 3.5 KiB · JavaScript
Last changed by
Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
History of this fileTake it
Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
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 back9Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 10–34why, one step back10export const PROXY_STATE = Symbol("immer-proxy-state")1112let autoFreeze = true13let 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}34Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 35–53why, one step back35export 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}53Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 54–55why, one step back54export 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 back56}Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 57why, one step back57Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 58–87why, one step back58export 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}6566// given a base object, returns it if unmodified, or return the changed cloned if modified67export function finalize(base) {68 if (isProxy(base)) {69 const state = base[PROXY_STATE]70 if (state.modified === true) {71 if (state.finalized === true) return state.copy72 state.finalized = true73 if (Array.isArray(state.base))74 return useProxies75 ? finalizeArrayProxy(state)76 : finalizeArrayEs5(base, state)77 return useProxies78 ? finalizeObjectProxy(state)79 : finalizeObjectEs5(base, state)80 } else return state.base81 } else if (base !== null && typeof base === "object") {82 finalizeNonProxiedObject(base)83 }84 return base85}8687function finalizeNonProxiedObject(parent) {Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 88–95why, one step back88 // 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 original89 // 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)) return92 if (Array.isArray(parent)) {93 for (let i = 0; i < parent.length; i++) {94 const child = parent[i]95 if (isProxy(child)) {Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 96–97why, one step back96 parent[i] = finalize(child)97 } else finalizeNonProxiedObject(child)Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 98–102why, one step back98 }99 } else {100 for (let key in parent) {101 const child = parent[key]102 if (isProxy(child)) {Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 103–104why, one step back103 parent[key] = finalize(child)104 } else finalizeNonProxiedObject(child)Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 105–106why, one step back105 }106 }Merge branch 'master' into masterMichel Weststratebefore adoption8ecb21e
lines 107–116why, one step back107 // always freeze completely new data108 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 back117}