Skip to content

Commit 4e31970

Browse files
committed
fix(reactivity): unexpected behaviors for array index out of valid array length when set and del
1 parent 30e3ddf commit 4e31970

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
@@ -60,9 +60,15 @@ export function isArray<T>(x: unknown): x is T[] {
6060
return Array.isArray(x)
6161
}
6262

63+
const MAX_VALID_ARRAY_LENGTH = 4294967295 // Math.pow(2, 32) - 1
6364
export function isValidArrayIndex(val: any): boolean {
6465
const n = parseFloat(String(val))
65-
return n >= 0 && Math.floor(n) === n && isFinite(val)
66+
return (
67+
n >= 0 &&
68+
Math.floor(n) === n &&
69+
isFinite(val) &&
70+
n <= MAX_VALID_ARRAY_LENGTH
71+
)
6672
}
6773

6874
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)