@@ -5,7 +5,7 @@ import { InputFieldFactory } from './inputFields/InputFieldFactory';
55import { InputFieldArgumentType , InputFieldDeclaration , InputFieldDeclarationParser } from './parsers/InputFieldDeclarationParser' ;
66import { AbstractInputFieldArgument } from './inputFieldArguments/AbstractInputFieldArgument' ;
77import { ClassInputFieldArgument } from './inputFieldArguments/ClassInputFieldArgument' ;
8- import { getFrontmatterOfTFile , updateOrInsertFieldInTFile } from '@opd-libs/opd-metadata-lib/lib/API' ;
8+ import { getFrontmatterOfTFile } from '@opd-libs/opd-metadata-lib/lib/API' ;
99import { traverseObject , validatePath as validateObjectPath } from '@opd-libs/opd-metadata-lib/lib/Utils' ;
1010import { MetaBindBindTargetError , MetaBindInternalError } from './utils/MetaBindErrors' ;
1111
@@ -18,7 +18,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
1818 plugin : MetaBindPlugin ;
1919 metaData : any ;
2020 filePath : string ;
21- uid : number ;
21+ uuid : string ;
2222 inputField : AbstractInputField | undefined ;
2323 error : string ;
2424 type : InputFieldMarkdownRenderChildType ;
@@ -28,17 +28,16 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
2828 bindTargetFile : TFile | undefined ;
2929 bindTargetMetadataField : string | undefined ;
3030
31- limitInterval : number | undefined ;
3231 intervalCounter : number ;
3332 metadataValueUpdateQueue : any [ ] ;
3433 inputFieldValueUpdateQueue : any [ ] ;
3534
36- constructor ( containerEl : HTMLElement , type : InputFieldMarkdownRenderChildType , fullDeclaration : string , plugin : MetaBindPlugin , filePath : string , uid : number ) {
35+ constructor ( containerEl : HTMLElement , type : InputFieldMarkdownRenderChildType , fullDeclaration : string , plugin : MetaBindPlugin , filePath : string , uuid : string ) {
3736 super ( containerEl ) ;
3837
3938 this . error = '' ;
4039 this . filePath = filePath ;
41- this . uid = uid ;
40+ this . uuid = uuid ;
4241 this . plugin = plugin ;
4342 this . type = type ;
4443 this . fullDeclaration = fullDeclaration ;
@@ -58,10 +57,8 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
5857 this . inputField = InputFieldFactory . createInputField ( this . inputFieldDeclaration . inputFieldType , {
5958 type : type ,
6059 inputFieldMarkdownRenderChild : this ,
61- onValueChanged : this . pushToMetadataValueUpdateQueue . bind ( this ) ,
60+ onValueChanged : this . updateMetadataManager . bind ( this ) ,
6261 } ) ;
63-
64- this . limitInterval = window . setInterval ( ( ) => this . applyValueUpdateQueues ( ) , this . plugin . settings . syncInterval ) ;
6562 } catch ( e : any ) {
6663 this . error = e . message ;
6764 console . warn ( e ) ;
@@ -109,85 +106,47 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
109106 }
110107 }
111108
112- // use this interval to reduce writing operations
113- async applyValueUpdateQueues ( ) : Promise < void > {
114- if ( this . metadataValueUpdateQueue . length !== 0 ) {
115- console . debug ( `meta-bind | applying to metadataUpdateQueue to field ${ this . uid } ` ) ;
116- await this . applyMetadataValueUpdateQueue ( ) ;
117- this . cleanUpUpdateQueues ( ) ;
109+ registerSelfToMetadataManager ( ) : void {
110+ if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataField ) {
118111 return ;
119112 }
120113
121- if ( this . inputFieldValueUpdateQueue . length !== 0 ) {
122- console . debug ( `meta-bind | applying to inputFieldValueUpdateQueue to field ${ this . uid } ` ) ;
123- await this . applyInputFieldValueUpdateQueue ( ) ;
124- this . cleanUpUpdateQueues ( ) ;
125- return ;
126- }
114+ this . plugin . metadataManager . register (
115+ this . bindTargetFile ,
116+ metadata => {
117+ if ( ! this . inputField ) {
118+ throw new MetaBindInternalError ( 'inputField is undefined, can not update inputField' ) ;
119+ }
120+
121+ const value = traverseObject ( this . bindTargetMetadataField as string , metadata ) ;
122+ if ( ! this . inputField . isEqualValue ( value ) ) {
123+ this . inputField . setValue ( value ) ;
124+ }
125+ } ,
126+ this . uuid
127+ ) ;
127128 }
128129
129- async applyMetadataValueUpdateQueue ( ) : Promise < void > {
130- if ( ! this . inputFieldDeclaration ) {
131- throw new MetaBindInternalError ( 'inputFieldDeclaration is undefined, can not update metadata' ) ;
132- }
133- if ( ! this . inputFieldDeclaration . isBound ) {
130+ unregisterSelfFromMetadataManager ( ) : void {
131+ if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataField ) {
134132 return ;
135133 }
136- if ( ! this . bindTargetMetadataField || ! this . bindTargetFile ) {
137- throw new MetaBindInternalError ( 'bindTargetMetadataField or bindTargetFile is undefined, can not update metadata' ) ;
138- }
139-
140- if ( this . metadataValueUpdateQueue . length > 0 ) {
141- await updateOrInsertFieldInTFile ( this . bindTargetMetadataField , this . metadataValueUpdateQueue . at ( - 1 ) , this . bindTargetFile , this . plugin ) ;
142- } else {
143- throw new MetaBindInternalError ( `cannot apply metadataValueUpdateQueue to inputField ${ this . uid } , metadataValueUpdateQueue is empty` ) ;
144- }
145- }
146-
147- async applyInputFieldValueUpdateQueue ( ) : Promise < void > {
148- if ( ! this . inputFieldDeclaration ) {
149- throw new MetaBindInternalError ( 'inputFieldDeclaration is undefined, can not update inputField' ) ;
150- }
151- if ( ! this . inputField ) {
152- throw new MetaBindInternalError ( 'inputField is undefined, can not update inputField' ) ;
153- }
154-
155- if ( this . inputFieldValueUpdateQueue . length > 0 ) {
156- let value = this . inputFieldValueUpdateQueue . at ( - 1 ) ;
157-
158- if ( value == null ) {
159- value = this . inputField . getDefaultValue ( ) ;
160- }
161-
162- this . inputField . setValue ( value ) ;
163- } else {
164- throw new MetaBindInternalError ( `cannot apply inputFieldValueUpdateQueue to inputField ${ this . uid } , inputFieldValueUpdateQueue is empty` ) ;
165- }
166- }
167134
168- cleanUpUpdateQueues ( ) : void {
169- this . metadataValueUpdateQueue = [ ] ;
170- this . inputFieldValueUpdateQueue = [ ] ;
135+ this . plugin . metadataManager . unregister ( this . bindTargetFile , this . uuid ) ;
171136 }
172137
173- pushToMetadataValueUpdateQueue ( value : any ) : void {
174- if ( this . inputFieldDeclaration ?. isBound ) {
175- console . debug ( `meta-bind | pushed value ${ value } (typeof ${ typeof value } ) to metadataUpdateQueue on field ${ this . uid } ` ) ;
176- this . metadataValueUpdateQueue . push ( value ) ;
138+ updateMetadataManager ( value : any ) : void {
139+ if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataField ) {
140+ return ;
177141 }
178- }
179142
180- pushToInputFieldValueUpdateQueue ( value : any ) : void {
181- if ( ! this . inputField ?. isEqualValue ( value ) ) {
182- console . debug ( `meta-bind | pushed value ${ value } (typeof ${ typeof value } ) to inputFieldValueUpdateQueue on field ${ this . uid } ` ) ;
183- this . inputFieldValueUpdateQueue . push ( value ) ;
184- }
143+ this . plugin . metadataManager . updatePropertyInMetadataFileCache ( value , this . bindTargetMetadataField , this . bindTargetFile , this . uuid ) ;
185144 }
186145
187146 getInitialValue ( ) : any | undefined {
188147 if ( this . inputFieldDeclaration ?. isBound && this . bindTargetMetadataField ) {
189148 const value = traverseObject ( this . bindTargetMetadataField , this . metaData ) ;
190- console . debug ( `meta-bind | setting initial value to ${ value } (typeof ${ typeof value } ) for input field ${ this . uid } ` ) ;
149+ console . debug ( `meta-bind | setting initial value to ${ value } (typeof ${ typeof value } ) for input field ${ this . uuid } ` ) ;
191150 return value ?? this . inputField ?. getDefaultValue ( ) ;
192151 }
193152 }
@@ -231,6 +190,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
231190 return ;
232191 }
233192
193+ this . registerSelfToMetadataManager ( ) ;
234194 this . plugin . registerInputFieldMarkdownRenderChild ( this ) ;
235195
236196 this . inputField . render ( container ) ;
@@ -248,9 +208,8 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
248208 console . debug ( 'meta-bind | unload inputFieldMarkdownRenderChild' , this ) ;
249209
250210 this . plugin . unregisterInputFieldMarkdownRenderChild ( this ) ;
211+ this . unregisterSelfFromMetadataManager ( ) ;
251212
252213 super . onunload ( ) ;
253-
254- window . clearInterval ( this . limitInterval ) ;
255214 }
256215}
0 commit comments