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

Function apply

packages/reactivity/src/arrayInstrumentations.ts:270–306  ·  view source on GitHub ↗
(
  self: unknown[],
  method: ArrayMethods,
  fn: (item: unknown, index: number, array: unknown[]) => unknown,
  thisArg?: unknown,
  wrappedRetFn?: (result: any) => unknown,
  args?: IArguments,
)

Source from the content-addressed store, hash-verified

268// instrument functions that read (potentially) all items
269// to take ARRAY_ITERATE dependency
270function apply(
271 self: unknown[],
272 method: ArrayMethods,
273 fn: (item: unknown, index: number, array: unknown[]) => unknown,
274 thisArg?: unknown,
275 wrappedRetFn?: (result: any) => unknown,
276 args?: IArguments,
277) {
278 const arr = shallowReadArray(self)
279 const needsWrap = arr !== self && !isShallow(self)
280 // @ts-expect-error our code is limited to es2016 but user code is not
281 const methodFn = arr[method]
282
283 // #11759
284 // If the method being called is from a user-extended Array, the arguments will be unknown
285 // (unknown order and unknown parameter types). In this case, we skip the shallowReadArray
286 // handling and directly call apply with self.
287 if (methodFn !== arrayProto[method as any]) {
288 const result = methodFn.apply(self, args)
289 return needsWrap ? toReactive(result) : result
290 }
291
292 let wrappedFn = fn
293 if (arr !== self) {
294 if (needsWrap) {
295 wrappedFn = function (this: unknown, item, index) {
296 return fn.call(this, toWrapped(self, item), index, self)
297 }
298 } else if (fn.length > 2) {
299 wrappedFn = function (this: unknown, item, index) {
300 return fn.call(this, item, index, self)
301 }
302 }
303 }
304 const result = methodFn.call(arr, wrappedFn, thisArg)
305 return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result
306}
307
308// instrument reduce and reduceRight to take ARRAY_ITERATE dependency
309function reduce(

Callers 9

everyFunction · 0.85
filterFunction · 0.85
findFunction · 0.85
findIndexFunction · 0.85
findLastFunction · 0.85
findLastIndexFunction · 0.85
forEachFunction · 0.85
mapFunction · 0.85
someFunction · 0.85

Calls 4

isShallowFunction · 0.90
toReactiveFunction · 0.90
shallowReadArrayFunction · 0.85
toWrappedFunction · 0.85

Tested by

no test coverage detected