Skip to content

Commit

Permalink
fix(reactivity): handle non-array arguments in reactive concat meth…
Browse files Browse the repository at this point in the history
…od (#11794)

close #11792
  • Loading branch information
jh-leong committed Sep 4, 2024
1 parent 6402b98 commit 475977a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 21 additions & 5 deletions packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,35 @@ describe('reactivity/reactive/Array', () => {
const a2 = reactive([{ val: 3 }])
const a3 = [4, 5]

let result = computed(() => a1.concat(a2, a3))
expect(result.value).toStrictEqual([1, { val: 2 }, { val: 3 }, 4, 5])
let result = computed(() => a1.concat(a2, a3, 6, { val: 7 }))
expect(result.value).toStrictEqual([
1,
{ val: 2 },
{ val: 3 },
4,
5,
6,
{ val: 7 },
])
expect(isReactive(result.value[1])).toBe(false)
expect(isReactive(result.value[2])).toBe(true)
expect(isReactive(result.value[6])).toBe(false)

a1.shift()
expect(result.value).toStrictEqual([{ val: 2 }, { val: 3 }, 4, 5])
expect(result.value).toStrictEqual([
{ val: 2 },
{ val: 3 },
4,
5,
6,
{ val: 7 },
])

a2.pop()
expect(result.value).toStrictEqual([{ val: 2 }, 4, 5])
expect(result.value).toStrictEqual([{ val: 2 }, 4, 5, 6, { val: 7 }])

a3.pop()
expect(result.value).toStrictEqual([{ val: 2 }, 4, 5])
expect(result.value).toStrictEqual([{ val: 2 }, 4, 5, 6, { val: 7 }])
})

test('entries', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/reactivity/src/arrayInstrumentations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TrackOpTypes } from './constants'
import { endBatch, pauseTracking, resetTracking, startBatch } from './effect'
import { isProxy, isShallow, toRaw, toReactive } from './reactive'
import { ARRAY_ITERATE_KEY, track } from './dep'
import { isArray } from '@vue/shared'

/**
* Track array iteration and return:
Expand Down Expand Up @@ -30,9 +31,9 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
return iterator(this, Symbol.iterator, toReactive)
},

concat(...args: unknown[][]) {
concat(...args: unknown[]) {
return reactiveReadArray(this).concat(
...args.map(x => reactiveReadArray(x)),
...args.map(x => (isArray(x) ? reactiveReadArray(x) : x)),
)
},

Expand Down

0 comments on commit 475977a

Please sign in to comment.