From 7e60d1058ff06e3d37c8608f3449453321220edc Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 12 Dec 2023 13:49:23 +0800 Subject: [PATCH] perf: use sync watcher for defineModel local mode ref https://github.com/vuejs/rfcs/discussions/503#discussioncomment-7566278 --- packages/runtime-core/src/apiSetupHelpers.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/runtime-core/src/apiSetupHelpers.ts b/packages/runtime-core/src/apiSetupHelpers.ts index 2c2f0d8326b..76c5aef4eaf 100644 --- a/packages/runtime-core/src/apiSetupHelpers.ts +++ b/packages/runtime-core/src/apiSetupHelpers.ts @@ -31,7 +31,7 @@ import { import { warn } from './warning' import { SlotsType, StrictUnwrapSlotsType } from './componentSlots' import { Ref, ref } from '@vue/reactivity' -import { watch } from './apiWatch' +import { watch, watchSyncEffect } from './apiWatch' // dev only const warnRuntimeUsage = (method: string) => @@ -378,18 +378,20 @@ export function useModel( if (options && options.local) { const proxy = ref(props[name]) + watchSyncEffect(() => { + proxy.value = props[name] + }) watch( - () => props[name], - v => (proxy.value = v) + proxy, + value => { + if (value !== props[name]) { + i.emit(`update:${name}`, value) + } + }, + { flush: 'sync' } ) - watch(proxy, value => { - if (value !== props[name]) { - i.emit(`update:${name}`, value) - } - }) - return proxy } else { return {