WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 95d2d89

Browse files
committed
fix(macros): sync localValue with prop changes for defineModel
1 parent 6cf6955 commit 95d2d89

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/macros/src/core/helper/use-model.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ type DefineModelOptions<T = Record<string, any>> = {
55
get?: (v: T) => any
66
set?: (v: T) => any
77
}
8+
9+
const EMPTY_OBJ = {}
10+
811
export function useModel<
912
M extends PropertyKey,
1013
T extends Record<string, any>,
@@ -17,11 +20,14 @@ export function useModel(
1720
): any {
1821
const res = customRef((track, trigger) => {
1922
let localValue: any = options && options.default
20-
let prevEmittedValue: any
23+
let prevSetValue = EMPTY_OBJ
2124

2225
watchSyncEffect(() => {
23-
const propValue = props[name]
24-
if (!Object.is(prevEmittedValue, propValue)) {
26+
let propValue = props[name]
27+
if (propValue === undefined) {
28+
propValue = options && options.default
29+
}
30+
if (!Object.is(localValue, propValue)) {
2531
localValue = propValue
2632
trigger()
2733
}
@@ -34,15 +40,18 @@ export function useModel(
3440
},
3541

3642
set(value) {
37-
if (Object.is(value, localValue)) return
38-
localValue = value
43+
const emittedValue = options.set ? options.set(value) : value
44+
if (
45+
Object.is(emittedValue, localValue) &&
46+
(prevSetValue === EMPTY_OBJ || Object.is(value, prevSetValue))
47+
)
48+
return
49+
localValue = emittedValue
3950
trigger()
40-
const emittedValue = (prevEmittedValue = options.set
41-
? options.set(value)
42-
: value)
4351
for (const emit of [props[`onUpdate:${name}`]].flat()) {
4452
if (typeof emit === 'function') emit(emittedValue)
4553
}
54+
prevSetValue = value
4655
},
4756
}
4857
})

0 commit comments

Comments
 (0)