Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(reactivity): unexpected behaviors for array index out of valid ar…
…ray length when set and del (#719)
  • Loading branch information
hyf0 committed Jun 4, 2021
1 parent 9c03a45 commit f08a1d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/utils/utils.ts
Expand Up @@ -71,9 +71,15 @@ export const isMap = (val: unknown): val is Map<any, any> =>
export const isSet = (val: unknown): val is Set<any> =>
toTypeString(val) === '[object Set]'

const MAX_VALID_ARRAY_LENGTH = 4294967295 // Math.pow(2, 32) - 1
export function isValidArrayIndex(val: any): boolean {
const n = parseFloat(String(val))
return n >= 0 && Math.floor(n) === n && isFinite(val)
return (
n >= 0 &&
Math.floor(n) === n &&
isFinite(val) &&
n <= MAX_VALID_ARRAY_LENGTH
)
}

export function isObject(val: unknown): val is Record<any, any> {
Expand Down
19 changes: 18 additions & 1 deletion test/v3/reactivity/del.spec.ts
@@ -1,4 +1,4 @@
import { del, reactive, ref, watch } from '../../../src'
import { del, reactive, ref, watch, set, watchEffect } from '../../../src'

// Vue.delete workaround for triggering view updates on object property/array index deletion
describe('reactivity/del', () => {
Expand Down Expand Up @@ -41,4 +41,21 @@ describe('reactivity/del', () => {
expect(spy).toBeCalledTimes(1)
expect(arr.value).toEqual([1, 3])
})

it('should trigger reactivity when using del on array to delete index out of valid array length', () => {
const arr = ref<number[]>([])
const MAX_VALID_ARRAY_LENGTH = Math.pow(2, 32) - 1
const NON_VALIDD_INDEX = MAX_VALID_ARRAY_LENGTH + 1
set(arr.value, NON_VALIDD_INDEX, 0)
const spy = jest.fn()
watchEffect(
() => {
spy(arr.value)
},
{ flush: 'sync' }
)
expect(spy).toBeCalledTimes(1)
del(arr.value, NON_VALIDD_INDEX)
expect(spy).toBeCalledTimes(2)
})
})

0 comments on commit f08a1d6

Please sign in to comment.