MCPcopy
hub / github.com/vuejs/composition-api / shallowReactive

Function shallowReactive

src/reactivity/reactive.ts:178–230  ·  view source on GitHub ↗
(obj: any)

Source from the content-addressed store, hash-verified

176
177export function shallowReactive<T extends object = any>(obj: T): T
178export function shallowReactive(obj: any) {
179 if (!isObject(obj)) {
180 if (__DEV__) {
181 warn('"shallowReactive()" must be called on an object.')
182 }
183 return obj
184 }
185
186 if (
187 !(isPlainObject(obj) || isArray(obj)) ||
188 isRaw(obj) ||
189 !Object.isExtensible(obj)
190 ) {
191 return obj
192 }
193
194 const observed = observe(isArray(obj) ? [] : {})
195
196 const ob = (observed as any).__ob__
197
198 for (const key of Object.keys(obj)) {
199 let val = obj[key]
200 let getter: (() => any) | undefined
201 let setter: ((x: any) => void) | undefined
202 const property = Object.getOwnPropertyDescriptor(obj, key)
203 if (property) {
204 if (property.configurable === false) {
205 continue
206 }
207 getter = property.get
208 setter = property.set
209 }
210
211 proxy(observed, key, {
212 get: function getterHandler() {
213 ob.dep?.depend()
214 return val
215 },
216 set: function setterHandler(newVal: any) {
217 if (getter && !setter) return
218
219 if (!isForceTrigger() && val === newVal) return
220 if (setter) {
221 setter.call(obj, newVal)
222 } else {
223 val = newVal
224 }
225 ob.dep?.notify()
226 },
227 })
228 }
229 return observed
230}
231
232/**
233 * Make obj reactivity

Callers 4

reactive.spec.tsFile · 0.90
assertValueFunction · 0.90
apiWatch.spec.tsFile · 0.90
shallowRefFunction · 0.90

Calls 8

isObjectFunction · 0.90
warnFunction · 0.90
isPlainObjectFunction · 0.90
isArrayFunction · 0.90
proxyFunction · 0.90
isForceTriggerFunction · 0.90
isRawFunction · 0.85
observeFunction · 0.85

Tested by 1

assertValueFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…