Skip to content

refactor(reactivity): simplify the wrapping logic for returned values #11434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/reactivity/src/arrayInstrumentations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
fn: (item: unknown, index: number, array: unknown[]) => unknown,
thisArg?: unknown,
) {
const result = apply(this, 'filter', fn, thisArg)
return isProxy(this) && !isShallow(this) ? result.map(toReactive) : result
return apply(this, 'filter', fn, thisArg, v => v.map(toReactive))
},

find(
fn: (item: unknown, index: number, array: unknown[]) => boolean,
thisArg?: unknown,
) {
const result = apply(this, 'find', fn, thisArg)
return isProxy(this) && !isShallow(this) ? toReactive(result) : result
return apply(this, 'find', fn, thisArg, toReactive)
},

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

findLastIndex(
Expand Down Expand Up @@ -237,11 +234,14 @@ function apply(
method: ArrayMethods,
fn: (item: unknown, index: number, array: unknown[]) => unknown,
thisArg?: unknown,
wrappedRetFn?: (result: any) => unknown,
) {
const arr = shallowReadArray(self)
let needsWrap = false
let wrappedFn = fn
if (arr !== self) {
if (!isShallow(self)) {
needsWrap = !isShallow(self)
if (needsWrap) {
wrappedFn = function (this: unknown, item, index) {
return fn.call(this, toReactive(item), index, self)
}
Expand All @@ -252,7 +252,8 @@ function apply(
}
}
// @ts-expect-error our code is limited to es2016 but user code is not
return arr[method](wrappedFn, thisArg)
const result = arr[method](wrappedFn, thisArg)
return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result
}

// instrument reduce and reduceRight to take ARRAY_ITERATE dependency
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function renderList(
let ret: VNodeChild[]
const cached = (cache && cache[index!]) as VNode[] | undefined
const sourceIsArray = isArray(source)
const sourceIsReactiveArray = sourceIsArray && isReactive(source)

if (sourceIsArray || isString(source)) {
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
if (sourceIsReactiveArray) {
source = shallowReadArray(source)
}
Expand Down