From 0e5a3c47a7398dfd0107fccf9b615772dd01aa74 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 19 Oct 2020 17:37:10 -0400 Subject: [PATCH] fix(reactivity): track length on for in iteration on Array fix #2427 --- .../reactivity/__tests__/reactiveArray.spec.ts | 15 +++++++++++++++ packages/reactivity/src/baseHandlers.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/__tests__/reactiveArray.spec.ts b/packages/reactivity/__tests__/reactiveArray.spec.ts index 170cb1b3..da3dc1f1 100644 --- a/packages/reactivity/__tests__/reactiveArray.spec.ts +++ b/packages/reactivity/__tests__/reactiveArray.spec.ts @@ -128,6 +128,21 @@ describe('reactivity/reactive/Array', () => { expect(fn).toHaveBeenCalledTimes(1) }) + // #2427 + test('track length on for ... in iteration', () => { + const array = reactive([1]) + let length = '' + effect(() => { + length = '' + for (const key in array) { + length += key + } + }) + expect(length).toBe('0') + array.push(1) + expect(length).toBe('01') + }) + describe('Array methods w/ refs', () => { let original: any[] beforeEach(() => { diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 3d5ee55f..a246b6f3 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -179,7 +179,7 @@ function has(target: object, key: string | symbol): boolean { } function ownKeys(target: object): (string | number | symbol)[] { - track(target, TrackOpTypes.ITERATE, ITERATE_KEY) + track(target, TrackOpTypes.ITERATE, isArray(target) ? 'length' : ITERATE_KEY) return Reflect.ownKeys(target) }