Skip to content

Commit f08a1d6

Browse files
authored
fix(reactivity): unexpected behaviors for array index out of valid array length when set and del (#719)
1 parent 9c03a45 commit f08a1d6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/utils/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ export const isMap = (val: unknown): val is Map<any, any> =>
7171
export const isSet = (val: unknown): val is Set<any> =>
7272
toTypeString(val) === '[object Set]'
7373

74+
const MAX_VALID_ARRAY_LENGTH = 4294967295 // Math.pow(2, 32) - 1
7475
export function isValidArrayIndex(val: any): boolean {
7576
const n = parseFloat(String(val))
76-
return n >= 0 && Math.floor(n) === n && isFinite(val)
77+
return (
78+
n >= 0 &&
79+
Math.floor(n) === n &&
80+
isFinite(val) &&
81+
n <= MAX_VALID_ARRAY_LENGTH
82+
)
7783
}
7884

7985
export function isObject(val: unknown): val is Record<any, any> {

test/v3/reactivity/del.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { del, reactive, ref, watch } from '../../../src'
1+
import { del, reactive, ref, watch, set, watchEffect } from '../../../src'
22

33
// Vue.delete workaround for triggering view updates on object property/array index deletion
44
describe('reactivity/del', () => {
@@ -41,4 +41,21 @@ describe('reactivity/del', () => {
4141
expect(spy).toBeCalledTimes(1)
4242
expect(arr.value).toEqual([1, 3])
4343
})
44+
45+
it('should trigger reactivity when using del on array to delete index out of valid array length', () => {
46+
const arr = ref<number[]>([])
47+
const MAX_VALID_ARRAY_LENGTH = Math.pow(2, 32) - 1
48+
const NON_VALIDD_INDEX = MAX_VALID_ARRAY_LENGTH + 1
49+
set(arr.value, NON_VALIDD_INDEX, 0)
50+
const spy = jest.fn()
51+
watchEffect(
52+
() => {
53+
spy(arr.value)
54+
},
55+
{ flush: 'sync' }
56+
)
57+
expect(spy).toBeCalledTimes(1)
58+
del(arr.value, NON_VALIDD_INDEX)
59+
expect(spy).toBeCalledTimes(2)
60+
})
4461
})

0 commit comments

Comments
 (0)