Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): enable trigger when use str to set length of arr #6810

Merged
merged 2 commits into from Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Expand Up @@ -922,6 +922,22 @@ describe('reactivity/effect', () => {
expect(fnSpy2).toHaveBeenCalledTimes(1)
})

it('should be triggered when set length with string', () => {
let ret1 = 'idle'
let ret2 = 'idle'
const arr1 = reactive(new Array(11).fill(0))
const arr2 = reactive(new Array(11).fill(0))
effect(() => {
ret1 = arr1[10] === undefined ? 'arr[10] is set to empty' : 'idle'
})
effect(() => {
ret2 = arr2[10] === undefined ? 'arr[10] is set to empty' : 'idle'
})
arr1.length = 2
arr2.length = '2' as any
expect(ret1).toBe(ret2)
})

describe('readonly + reactive for Map', () => {
test('should work with readonly(reactive(Map))', () => {
const m = reactive(new Map())
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/effect.ts
@@ -1,5 +1,5 @@
import { TrackOpTypes, TriggerOpTypes } from './operations'
import { extend, isArray, isIntegerKey, isMap } from '@vue/shared'
import { extend, isArray, isIntegerKey, isMap, toNumber } from '@vue/shared'
import { EffectScope, recordEffectScope } from './effectScope'
import {
createDep,
Expand Down Expand Up @@ -277,7 +277,7 @@ export function trigger(
deps = [...depsMap.values()]
} else if (key === 'length' && isArray(target)) {
depsMap.forEach((dep, key) => {
if (key === 'length' || key >= (newValue as number)) {
if (key === 'length' || key >= toNumber(newValue)) {
deps.push(dep)
}
})
Expand Down