]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): enable trigger when use str to set length of arr (#6810)
authorbcq028 <108849949+bcq028@users.noreply.github.com>
Fri, 21 Oct 2022 07:25:06 +0000 (15:25 +0800)
committerGitHub <noreply@github.com>
Fri, 21 Oct 2022 07:25:06 +0000 (09:25 +0200)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
packages/reactivity/__tests__/effect.spec.ts
packages/reactivity/src/effect.ts

index a66aff278a4eeca3c54b50c021f767685c36ce62..a322c7209f4dc4dd90a4cce612424804812564bb 100644 (file)
@@ -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())
index 34b53eb8fef103354e236eea8f2b508208a15df2..8a54372cd5b5032dae969525591b5a777033bbfa 100644 (file)
@@ -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,
@@ -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)
       }
     })