-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
common.js
184 lines (162 loc) · 5.33 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {generatePatches} from "./patches"
export const PROXY_STATE =
typeof Symbol !== "undefined"
? Symbol("immer-proxy-state")
: "__$immer_state"
export const RETURNED_AND_MODIFIED_ERROR =
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."
function verifyMinified() {}
const inProduction =
(typeof process !== "undefined" && process.env.NODE_ENV === "production") ||
verifyMinified.name !== "verifyMinified"
let autoFreeze = !inProduction
let useProxies = typeof Proxy !== "undefined"
/**
* Automatically freezes any state trees generated by immer.
* This protects against accidental modifications of the state tree outside of an immer function.
* This comes with a performance impact, so it is recommended to disable this option in production.
* It is by default enabled.
*
* @returns {void}
*/
export function setAutoFreeze(enableAutoFreeze) {
autoFreeze = enableAutoFreeze
}
export function setUseProxies(value) {
useProxies = value
}
export function getUseProxies() {
return useProxies
}
export function isProxy(value) {
return !!value && !!value[PROXY_STATE]
}
export function isProxyable(value) {
if (!value) return false
if (typeof value !== "object") return false
if (Array.isArray(value)) return true
const proto = Object.getPrototypeOf(value)
return proto === null || proto === Object.prototype
}
export function freeze(value) {
if (autoFreeze) {
Object.freeze(value)
}
return value
}
const assign =
Object.assign ||
function assign(target, value) {
for (let key in value) {
if (has(value, key)) {
target[key] = value[key]
}
}
return target
}
export function shallowCopy(value) {
if (Array.isArray(value)) {
return assign([], value)
}
const target = value.__proto__ === undefined ? Object.create(null) : {}
return assign(target, value)
}
export function each(value, cb) {
for (let key in value) {
if (Array.isArray(value) && isNonNegativeInteger(key)) key = Number(key)
cb(key, value[key])
}
}
export function diffKeys(from, to) {
// TODO: optimize
const a = Object.keys(from)
const b = Object.keys(to)
return {
added: b.filter(key => a.indexOf(key) === -1),
removed: a.filter(key => b.indexOf(key) === -1)
}
}
export function has(thing, prop) {
return Object.prototype.hasOwnProperty.call(thing, prop)
}
// given a base object, returns it if unmodified, or return the changed cloned if modified
export function finalize(base, path, patches, inversePatches) {
if (isProxy(base)) {
const state = base[PROXY_STATE]
if (state.modified === true) {
if (state.finalized === true) return state.copy
state.finalized = true
const result = finalizeObject(
useProxies ? state.copy : (state.copy = shallowCopy(base)),
state,
path,
patches,
inversePatches
)
generatePatches(
state,
path,
patches,
inversePatches,
state.base,
result
)
return result
} else {
return state.base
}
}
finalizeNonProxiedObject(base)
return base
}
function finalizeObject(copy, state, path, patches, inversePatches) {
const base = state.base
each(copy, (prop, value) => {
if (value !== base[prop]) {
// if there was an assignment on this property, we don't need to generate
// patches for the subtree
const generatePatches = patches && !has(state.assigned, prop)
copy[prop] = finalize(
value,
generatePatches && path.concat(prop),
generatePatches && patches,
inversePatches
)
}
})
return freeze(copy)
}
function finalizeNonProxiedObject(parent) {
// 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
// tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well
if (!isProxyable(parent)) return
if (Object.isFrozen(parent)) return
each(parent, (i, child) => {
if (isProxy(child)) {
parent[i] = finalize(child)
} else finalizeNonProxiedObject(child)
})
// always freeze completely new data
freeze(parent)
}
export function verifyReturnValue(returnedValue, proxy, isProxyModified) {
if (returnedValue !== undefined && returnedValue !== proxy) {
// something was returned, and it wasn't the proxy itself
if (isProxyModified)
throw new Error(
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."
)
}
}
export function is(x, y) {
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
if (x === y) {
return x !== 0 || 1 / x === 1 / y
} else {
return x !== x && y !== y
}
}
export function isNonNegativeInteger(v) {
const reg = /^\d+$/
return reg.test(v)
}