Skip to content

Commit e28c581

Browse files
authored
refactor(reactivity): simplify the wrapping logic for returned values in array instrumentations (#11434)
1 parent 94fb2b8 commit e28c581

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

packages/reactivity/src/arrayInstrumentations.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,14 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
5454
fn: (item: unknown, index: number, array: unknown[]) => unknown,
5555
thisArg?: unknown,
5656
) {
57-
const result = apply(this, 'filter', fn, thisArg)
58-
return isProxy(this) && !isShallow(this) ? result.map(toReactive) : result
57+
return apply(this, 'filter', fn, thisArg, v => v.map(toReactive))
5958
},
6059

6160
find(
6261
fn: (item: unknown, index: number, array: unknown[]) => boolean,
6362
thisArg?: unknown,
6463
) {
65-
const result = apply(this, 'find', fn, thisArg)
66-
return isProxy(this) && !isShallow(this) ? toReactive(result) : result
64+
return apply(this, 'find', fn, thisArg, toReactive)
6765
},
6866

6967
findIndex(
@@ -77,8 +75,7 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
7775
fn: (item: unknown, index: number, array: unknown[]) => boolean,
7876
thisArg?: unknown,
7977
) {
80-
const result = apply(this, 'findLast', fn, thisArg)
81-
return isProxy(this) && !isShallow(this) ? toReactive(result) : result
78+
return apply(this, 'findLast', fn, thisArg, toReactive)
8279
},
8380

8481
findLastIndex(
@@ -237,11 +234,14 @@ function apply(
237234
method: ArrayMethods,
238235
fn: (item: unknown, index: number, array: unknown[]) => unknown,
239236
thisArg?: unknown,
237+
wrappedRetFn?: (result: any) => unknown,
240238
) {
241239
const arr = shallowReadArray(self)
240+
let needsWrap = false
242241
let wrappedFn = fn
243242
if (arr !== self) {
244-
if (!isShallow(self)) {
243+
needsWrap = !isShallow(self)
244+
if (needsWrap) {
245245
wrappedFn = function (this: unknown, item, index) {
246246
return fn.call(this, toReactive(item), index, self)
247247
}
@@ -252,7 +252,8 @@ function apply(
252252
}
253253
}
254254
// @ts-expect-error our code is limited to es2016 but user code is not
255-
return arr[method](wrappedFn, thisArg)
255+
const result = arr[method](wrappedFn, thisArg)
256+
return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result
256257
}
257258

258259
// instrument reduce and reduceRight to take ARRAY_ITERATE dependency

packages/runtime-core/src/helpers/renderList.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export function renderList(
6060
let ret: VNodeChild[]
6161
const cached = (cache && cache[index!]) as VNode[] | undefined
6262
const sourceIsArray = isArray(source)
63-
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
6463

6564
if (sourceIsArray || isString(source)) {
65+
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
6666
if (sourceIsReactiveArray) {
6767
source = shallowReadArray(source)
6868
}

0 commit comments

Comments
 (0)