From a0801f4d23f71c4aa9bbbbc7e7eec43aa27e8502 Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 13 May 2022 08:20:04 +0800 Subject: [PATCH] fix(watch): fix watching multiple sources containing shallowRef (#5381) fix #5371 --- packages/runtime-core/__tests__/apiWatch.spec.ts | 15 +++++++++++++++ packages/runtime-core/src/apiWatch.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 1c6fb0a88df..118c5732681 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -892,6 +892,21 @@ describe('api: watch', () => { expect(sideEffect).toBe(2) }) + test('should force trigger on triggerRef when watching multiple sources: shallow ref array', async () => { + const v = shallowRef([] as any) + const spy = jest.fn() + watch([v], () => { + spy() + }) + + v.value.push(1) + triggerRef(v) + + await nextTick() + // should trigger now + expect(spy).toHaveBeenCalledTimes(1) + }) + // #2125 test('watchEffect should not recursively trigger itself', async () => { const spy = jest.fn() diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 84937eeae63..a1922f0cd2e 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -212,7 +212,7 @@ function doWatch( deep = true } else if (isArray(source)) { isMultiSource = true - forceTrigger = source.some(isReactive) + forceTrigger = source.some(s => isReactive(s) || isShallow(s)) getter = () => source.map(s => { if (isRef(s)) {