@@ -7,15 +7,20 @@ export function checkFieldInTemplate(template: string, field: string) {
77 return ( matches || [ ] ) . some ( ( m ) => m . includes ( field ) ) ;
88}
99
10- const checkFieldNeedsUpdate = ( field : string , newVal : Record < string , any > , oldVal : Record < string , any > ) => {
11- if ( oldVal [ field ] === newVal [ field ] ) {
12- return false ;
13- }
14- if ( JSON . stringify ( oldVal [ field ] ) === JSON . stringify ( newVal [ field ] ) ) {
15- return false ;
10+ /** Simple check which fields are used */
11+ function shouldUpdate ( template : string , computedField : string , val : Record < string , any > , oldVal : Record < string , any > ) {
12+ for ( const key of Object . keys ( val ) ) {
13+ if (
14+ key !== computedField &&
15+ checkFieldInTemplate ( template , key ) &&
16+ val [ key ] !== oldVal [ key ] &&
17+ JSON . stringify ( val [ key ] ) !== JSON . stringify ( oldVal [ key ] )
18+ ) {
19+ return true ;
20+ }
1621 }
17- return true ;
18- } ;
22+ return false ;
23+ }
1924
2025export const useCollectionRelations = ( collection : string ) : Ref < any [ ] > => {
2126 const { useRelationsStore } = useStores ( ) ;
@@ -33,18 +38,24 @@ export const useDeepValues = (
3338 values : Ref < Record < string , any > > ,
3439 relations : Ref < any [ ] > ,
3540 collection : string ,
41+ computedField : string ,
3642 pk : string ,
3743 template : string
3844) => {
3945 const api = useApi ( ) ;
40- const finalValues = ref < Record < string , any > > ( values . value ) ;
41- watch ( values , async ( val , oldVal ) => {
46+ const finalValues = ref < Record < string , any > > ( { } ) ;
47+ watch ( values , async ( ) => {
48+ if ( ! shouldUpdate ( template , computedField , values . value , finalValues . value ) ) {
49+ finalValues . value = values . value ;
50+ return ;
51+ }
52+
4253 const relationalData : Record < string , any > = { } ;
4354
4455 for ( const key of Object . keys ( values . value ) ) {
4556 const relation = relations . value . find ( ( rel ) => rel . meta ?. one_field === key ) ;
4657
47- if ( ! relation || ! checkFieldInTemplate ( template , key ) || ! checkFieldNeedsUpdate ( key , val , oldVal ) ) {
58+ if ( ! relation ) {
4859 continue ;
4960 }
5061
@@ -98,9 +109,7 @@ export const useDeepValues = (
98109 relationalData [ key ] = arrayOfData ;
99110 }
100111
101- if ( Object . keys ( relationalData ) . length ) {
102- finalValues . value = { ...values . value , ...relationalData } ;
103- }
112+ finalValues . value = { ...values . value , ...relationalData } ;
104113 } ) ;
105114
106115 return finalValues ;
0 commit comments