Skip to content
Myr

common.js

This file

Path

src/common.js

117 lines · 3.5 KiB · JavaScript

Last changed by

more code reuseMichel Weststratebefore adoption93372fc

History of this file

Take it

more code reuseMichel Weststratebefore adoption93372fc

lines 1–8why, one step back
1import {
2 finalizeArray as finalizeArrayProxy,3 finalizeObject as finalizeObjectProxy4} from "./proxy"5import {6 finalizeArray as finalizeArrayEs5,7 finalizeObject as finalizeObjectEs58} from "./es5"

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

line 9why, one step back
9

more code reuseMichel Weststratebefore adoption93372fc

lines 10–34why, one step back
10export 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}34

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

lines 35–53why, one step back
35export 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}53

small improvementsMichel Weststratebefore adoption7d0b7e8

lines 54–57why, one step back
54export function shallowCopy(value) {
55 return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?56}57

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 58–65why, one step back
58export 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}65

more code reuseMichel Weststratebefore adoption93372fc

lines 66–87why, one step back
66// given a base object, returns it if unmodified, or return the changed cloned if modified
67export 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) {

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 88–95why, 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)) return92 if (Array.isArray(parent)) {93 for (let i = 0; i < parent.length; i++) {94 const child = parent[i]95 if (isProxy(child)) {

more code reuseMichel Weststratebefore adoption93372fc

lines 96–97why, one step back
96 parent[i] = finalize(child)
97 } else finalizeNonProxiedObject(child)

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 98–102why, one step back
98 }
99 } else {100 for (let key in parent) {101 const child = parent[key]102 if (isProxy(child)) {

more code reuseMichel Weststratebefore adoption93372fc

lines 103–104why, one step back
103 parent[key] = finalize(child)
104 } else finalizeNonProxiedObject(child)

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 105–106why, one step back
105 }
106 }

Freeze non-proxied dataMichel Weststratebefore adoption5ac3ef7

lines 107–108why, one step back
107 // always freeze completely new data
108 freeze(parent)

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 109–111why, one step back
109}
110111export function verifyReturnValue(value) {

Freeze non-proxied dataMichel Weststratebefore adoption5ac3ef7

line 112why, one step back
112 // values either than undefined will trigger warning;

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 113–116why, one step back
113 if (value !== undefined)
114 console.warn(115 `Immer callback expects no return value. However ${typeof value} was returned`116 )

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

line 117why, one step back
117}