Skip to content

Commit 9de1d10

Browse files
authored
fix(reactivity): extended methods respect reactive (#11629)
close #11628
1 parent 5e0f6d5 commit 9de1d10

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/reactivity/__tests__/reactiveArray.spec.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,17 @@ describe('reactivity/reactive/Array', () => {
709709

710710
expect(state.things.every('foo', 'bar', 'baz')).toBe(false)
711711
expect(state.things.filter('foo', 'bar', 'baz')).toEqual([foo])
712-
expect(state.things.find('foo', 'bar', 'baz')).toBe(foo)
712+
713+
const _foo = state.things.find('foo', 'bar', 'baz')
714+
expect(isReactive(_foo)).toBe(true)
715+
expect(foo).toStrictEqual(_foo)
716+
713717
expect(state.things.findIndex('foo', 'bar', 'baz')).toBe(1)
714-
expect(state.things.findLast('foo', 'bar', 'baz')).toBe(bar)
718+
719+
const _bar = state.things.findLast('foo', 'bar', 'baz')
720+
expect(isReactive(_bar)).toBe(true)
721+
expect(bar).toStrictEqual(_bar)
722+
715723
expect(state.things.findLastIndex('foo', 'bar', 'baz')).toBe(1)
716724
expect(state.things.forEach('foo', 'bar', 'baz')).toBeUndefined()
717725
expect(state.things.map('foo', 'bar', 'baz')).toEqual(['1', '2', '3'])

packages/reactivity/src/arrayInstrumentations.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,17 @@ function apply(
239239
args?: IArguments,
240240
) {
241241
const arr = shallowReadArray(self)
242-
let methodFn
242+
const needsWrap = arr !== self && !isShallow(self)
243243
// @ts-expect-error our code is limited to es2016 but user code is not
244-
if ((methodFn = arr[method]) !== arrayProto[method]) {
245-
return methodFn.apply(arr, args)
244+
const methodFn = arr[method]
245+
// @ts-expect-error
246+
if (methodFn !== arrayProto[method]) {
247+
const result = methodFn.apply(arr, args)
248+
return needsWrap ? toReactive(result) : result
246249
}
247250

248-
let needsWrap = false
249251
let wrappedFn = fn
250252
if (arr !== self) {
251-
needsWrap = !isShallow(self)
252253
if (needsWrap) {
253254
wrappedFn = function (this: unknown, item, index) {
254255
return fn.call(this, toReactive(item), index, self)

0 commit comments

Comments
 (0)