From a3066581f3014aae31f2d96b96428100f1674166 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 6 Mar 2020 11:30:56 -0500 Subject: [PATCH] fix(reactivity): should not trigger length dependency on Array delete close #774 --- packages/reactivity/__tests__/reactiveArray.spec.ts | 11 +++++++++++ packages/reactivity/src/effect.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/__tests__/reactiveArray.spec.ts b/packages/reactivity/__tests__/reactiveArray.spec.ts index d91d4478..54218c19 100644 --- a/packages/reactivity/__tests__/reactiveArray.spec.ts +++ b/packages/reactivity/__tests__/reactiveArray.spec.ts @@ -88,6 +88,17 @@ describe('reactivity/reactive/Array', () => { expect(index).toBe(1) }) + test('delete on Array should not trigger length dependency', () => { + const arr = reactive([1, 2, 3]) + const fn = jest.fn() + effect(() => { + fn(arr.length) + }) + expect(fn).toHaveBeenCalledTimes(1) + delete arr[1] + expect(fn).toHaveBeenCalledTimes(1) + }) + describe('Array methods w/ refs', () => { let original: any[] beforeEach(() => { diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index 010aea00..2368caf6 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -196,7 +196,7 @@ export function trigger( // also run for iteration key on ADD | DELETE | Map.SET if ( type === TriggerOpTypes.ADD || - type === TriggerOpTypes.DELETE || + (type === TriggerOpTypes.DELETE && !isArray(target)) || (type === TriggerOpTypes.SET && target instanceof Map) ) { const iterationKey = isArray(target) ? 'length' : ITERATE_KEY