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

Class RefImpl

packages/reactivity/src/ref.ts:114–165  ·  view source on GitHub ↗

* @internal

Source from the content-addressed store, hash-verified

112 * @internal
113 */
114class RefImpl<T = any> {
115 _value: T
116 private _rawValue: T
117
118 dep: Dep = new Dep()
119
120 public readonly [ReactiveFlags.IS_REF] = true
121 public readonly [ReactiveFlags.IS_SHALLOW]: boolean = false
122
123 constructor(value: T, isShallow: boolean) {
124 this._rawValue = isShallow ? value : toRaw(value)
125 this._value = isShallow ? value : toReactive(value)
126 this[ReactiveFlags.IS_SHALLOW] = isShallow
127 }
128
129 get value() {
130 if (__DEV__) {
131 this.dep.track({
132 target: this,
133 type: TrackOpTypes.GET,
134 key: 'value',
135 })
136 } else {
137 this.dep.track()
138 }
139 return this._value
140 }
141
142 set value(newValue) {
143 const oldValue = this._rawValue
144 const useDirectValue =
145 this[ReactiveFlags.IS_SHALLOW] ||
146 isShallow(newValue) ||
147 isReadonly(newValue)
148 newValue = useDirectValue ? newValue : toRaw(newValue)
149 if (hasChanged(newValue, oldValue)) {
150 this._rawValue = newValue
151 this._value = useDirectValue ? newValue : toReactive(newValue)
152 if (__DEV__) {
153 this.dep.trigger({
154 target: this,
155 type: TriggerOpTypes.SET,
156 key: 'value',
157 newValue,
158 oldValue,
159 })
160 } else {
161 this.dep.trigger()
162 }
163 }
164 }
165}
166
167/**
168 * Force trigger effects that depends on a shallow ref. This is typically used

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected