Skip to content

Commit

Permalink
fix(reactivity): extended methods respect reactive (#11629)
Browse files Browse the repository at this point in the history
close #11628
  • Loading branch information
edison1105 authored Aug 16, 2024
1 parent 5e0f6d5 commit 9de1d10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 10 additions & 2 deletions packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
11 changes: 6 additions & 5 deletions packages/reactivity/src/arrayInstrumentations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9de1d10

Please sign in to comment.