MCPcopy
hub / github.com/vuejs/core / ObjectRefImpl

Class ObjectRefImpl

packages/reactivity/src/ref.ts:375–428  ·  packages/reactivity/src/ref.ts::ObjectRefImpl

Source from the content-addressed store, hash-verified

373}
374
375class ObjectRefImpl<T extends object, K extends keyof T> {
376 public readonly [ReactiveFlags.IS_REF] = true
377 public _value: T[K] = undefined!
378
379 private readonly _raw: T
380 private readonly _key: K
381 private readonly _shallow: boolean
382
383 constructor(
384 private readonly _object: T,
385 key: K,
386 private readonly _defaultValue?: T[K],
387 ) {
388 this._key = (isSymbol(key) ? key : String(key)) as K
389 this._raw = toRaw(_object)
390
391 let shallow = true
392 let obj = _object
393
394 class="cm">// For an array with integer key, refs are not unwrapped
395 if (!isArray(_object) || isSymbol(this._key) || !isIntegerKey(this._key)) {
396 class="cm">// Otherwise, check each proxy layer for unwrapping
397 do {
398 shallow = !isProxy(obj) || isShallow(obj)
399 } while (shallow && (obj = (obj as Target)[ReactiveFlags.RAW]))
400 }
401
402 this._shallow = shallow
403 }
404
405 get value() {
406 let val = this._object[this._key]
407 if (this._shallow) {
408 val = unref(val)
409 }
410 return (this._value = val === undefined ? this._defaultValue! : val)
411 }
412
413 set value(newVal) {
414 if (this._shallow && isRef(this._raw[this._key])) {
415 const nestedRef = this._object[this._key]
416 if (isRef(nestedRef)) {
417 nestedRef.value = newVal
418 return
419 }
420 }
421
422 this._object[this._key] = newVal
423 }
424
425 get dep(): Dep | undefined {
426 return getDepFromReactive(this._raw, this._key)
427 }
428}
429
430class GetterRefImpl<T> {
431 public readonly [ReactiveFlags.IS_REF] = true

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected