fix(reactivity): extended methods respect reactive (#11629)

close #11628
This commit is contained in:
edison 2024-08-16 16:23:09 +08:00 committed by GitHub
parent 5e0f6d5f8f
commit 9de1d101f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 7 deletions

View File

@ -709,9 +709,17 @@ describe('reactivity/reactive/Array', () => {
expect(state.things.every('foo', 'bar', 'baz')).toBe(false)
expect(state.things.filter('foo', 'bar', 'baz')).toEqual([foo])
expect(state.things.find('foo', 'bar', 'baz')).toBe(foo)
const _foo = state.things.find('foo', 'bar', 'baz')
expect(isReactive(_foo)).toBe(true)
expect(foo).toStrictEqual(_foo)
expect(state.things.findIndex('foo', 'bar', 'baz')).toBe(1)
expect(state.things.findLast('foo', 'bar', 'baz')).toBe(bar)
const _bar = state.things.findLast('foo', 'bar', 'baz')
expect(isReactive(_bar)).toBe(true)
expect(bar).toStrictEqual(_bar)
expect(state.things.findLastIndex('foo', 'bar', 'baz')).toBe(1)
expect(state.things.forEach('foo', 'bar', 'baz')).toBeUndefined()
expect(state.things.map('foo', 'bar', 'baz')).toEqual(['1', '2', '3'])

View File

@ -239,16 +239,17 @@ function apply(
args?: IArguments,
) {
const arr = shallowReadArray(self)
let methodFn
const needsWrap = arr !== self && !isShallow(self)
// @ts-expect-error our code is limited to es2016 but user code is not
if ((methodFn = arr[method]) !== arrayProto[method]) {
return methodFn.apply(arr, args)
const methodFn = arr[method]
// @ts-expect-error
if (methodFn !== arrayProto[method]) {
const result = methodFn.apply(arr, args)
return needsWrap ? toReactive(result) : result
}
let needsWrap = false
let wrappedFn = fn
if (arr !== self) {
needsWrap = !isShallow(self)
if (needsWrap) {
wrappedFn = function (this: unknown, item, index) {
return fn.call(this, toReactive(item), index, self)