From e9e2778e9ec5cca07c1df5f0c9b7b3595a1a3244 Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Sat, 21 Oct 2023 02:48:59 +0100 Subject: [PATCH] fix(reactivity): assigning array.length while observing a symbol property (#7568) --- packages/reactivity/__tests__/effect.spec.ts | 16 ++++++++++++++++ packages/reactivity/src/effect.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/__tests__/effect.spec.ts b/packages/reactivity/__tests__/effect.spec.ts index 635e6534abe..e34c7b31e40 100644 --- a/packages/reactivity/__tests__/effect.spec.ts +++ b/packages/reactivity/__tests__/effect.spec.ts @@ -243,6 +243,22 @@ describe('reactivity/effect', () => { expect(dummy).toBe(undefined) }) + it('should support manipulating an array while observing symbol keyed properties', () => { + const key = Symbol() + let dummy + const array: any = reactive([1, 2, 3]) + effect(() => (dummy = array[key])) + + expect(dummy).toBe(undefined) + array.pop() + array.shift() + array.splice(0, 1) + expect(dummy).toBe(undefined) + array[key] = 'value' + array.length = 0 + expect(dummy).toBe('value') + }) + it('should observe function valued properties', () => { const oldFunc = () => {} const newFunc = () => {} diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index bbac96a4b2a..c982dbd0b5a 100644 --- a/packages/reactivity/src/effect.ts +++ b/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, isSymbol } from '@vue/shared' import { EffectScope, recordEffectScope } from './effectScope' import { createDep, @@ -324,7 +324,7 @@ export function trigger( } else if (key === 'length' && isArray(target)) { const newLength = Number(newValue) depsMap.forEach((dep, key) => { - if (key === 'length' || key >= newLength) { + if (key === 'length' || (!isSymbol(key) && key >= newLength)) { deps.push(dep) } })