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

Method set

packages/reactivity/src/baseHandlers.ts:142–192  ·  view source on GitHub ↗
(
    target: Record<string | symbol, unknown>,
    key: string | symbol,
    value: unknown,
    receiver: object,
  )

Source from the content-addressed store, hash-verified

140 }
141
142 set(
143 target: Record<string | symbol, unknown>,
144 key: string | symbol,
145 value: unknown,
146 receiver: object,
147 ): boolean {
148 let oldValue = target[key]
149 const isArrayWithIntegerKey = isArray(target) && isIntegerKey(key)
150 if (!this._isShallow) {
151 const isOldValueReadonly = isReadonly(oldValue)
152 if (!isShallow(value) && !isReadonly(value)) {
153 oldValue = toRaw(oldValue)
154 value = toRaw(value)
155 }
156 if (!isArrayWithIntegerKey && isRef(oldValue) && !isRef(value)) {
157 if (isOldValueReadonly) {
158 if (__DEV__) {
159 warn(
160 `Set operation on key "${String(key)}" failed: target is readonly.`,
161 target[key],
162 )
163 }
164 return true
165 } else {
166 oldValue.value = value
167 return true
168 }
169 }
170 } else {
171 // in shallow mode, objects are set as-is regardless of reactive or not
172 }
173
174 const hadKey = isArrayWithIntegerKey
175 ? Number(key) < target.length
176 : hasOwn(target, key)
177 const result = Reflect.set(
178 target,
179 key,
180 value,
181 isRef(target) ? target : receiver,
182 )
183 // don't trigger if target is something up in the prototype chain of original
184 if (target === toRaw(receiver) && result) {
185 if (!hadKey) {
186 trigger(target, TriggerOpTypes.ADD, key, value)
187 } else if (hasChanged(value, oldValue)) {
188 trigger(target, TriggerOpTypes.SET, key, value, oldValue)
189 }
190 }
191 return result
192 }
193
194 deleteProperty(
195 target: Record<string | symbol, unknown>,

Callers 15

createMapFunction · 0.45
ref.tsFile · 0.45
onWatcherCleanupFunction · 0.45
traverseFunction · 0.45
createReactiveObjectFunction · 0.45
setFunction · 0.45
trackFunction · 0.45
reactive.spec.tsFile · 0.45
effect.spec.tsFile · 0.45
readonly.spec.tsFile · 0.45

Calls 10

isIntegerKeyFunction · 0.90
isReadonlyFunction · 0.90
isShallowFunction · 0.90
toRawFunction · 0.90
isRefFunction · 0.90
warnFunction · 0.90
hasOwnFunction · 0.90
triggerFunction · 0.90
hasChangedFunction · 0.90
StringInterface · 0.85

Tested by

no test coverage detected