@@ -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+
811export 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