@@ -33,7 +33,8 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
3333
3434 limitInterval : number | undefined ;
3535 intervalCounter : number ;
36- valueQueue : any [ ] ;
36+ metadataValueUpdateQueue : any [ ] ;
37+ inputFieldValueUpdateQueue : any [ ] ;
3738
3839 constructor ( containerEl : HTMLElement , type : InputFieldMarkdownRenderChildType , fullDeclaration : string , plugin : MetaBindPlugin , filePath : string , uid : number ) {
3940 super ( containerEl ) ;
@@ -45,7 +46,8 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
4546 this . type = type ;
4647 this . fullDeclaration = fullDeclaration ;
4748
48- this . valueQueue = [ ] ;
49+ this . metadataValueUpdateQueue = [ ] ;
50+ this . inputFieldValueUpdateQueue = [ ] ;
4951 this . intervalCounter = 0 ;
5052
5153 try {
@@ -60,10 +62,10 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
6062 this . inputField = InputFieldFactory . createInputField ( this . inputFieldDeclaration . inputFieldType , {
6163 type : type ,
6264 inputFieldMarkdownRenderChild : this ,
63- onValueChanged : this . pushToValueQueue . bind ( this ) ,
65+ onValueChanged : this . pushToMetadataValueUpdateQueue . bind ( this ) ,
6466 } ) ;
6567
66- this . limitInterval = window . setInterval ( ( ) => this . applyValueQueueToMetadata ( ) , this . plugin . settings . syncInterval ) ;
68+ this . limitInterval = window . setInterval ( ( ) => this . applyValueUpdateQueues ( ) , this . plugin . settings . syncInterval ) ;
6769 } catch ( e : any ) {
6870 this . error = e . message ;
6971 console . warn ( e ) ;
@@ -107,7 +109,21 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
107109 }
108110
109111 // use this interval to reduce writing operations
110- async applyValueQueueToMetadata ( ) : Promise < void > {
112+ async applyValueUpdateQueues ( ) : Promise < void > {
113+ if ( this . metadataValueUpdateQueue . length !== 0 ) {
114+ await this . applyMetadataValueUpdateQueue ( ) ;
115+ this . cleanUpUpdateQueues ( ) ;
116+ return ;
117+ }
118+
119+ if ( this . inputFieldValueUpdateQueue . length !== 0 ) {
120+ await this . applyInputFieldValueUpdateQueue ( ) ;
121+ this . cleanUpUpdateQueues ( ) ;
122+ return ;
123+ }
124+ }
125+
126+ async applyMetadataValueUpdateQueue ( ) : Promise < void > {
111127 if ( ! this . inputFieldDeclaration ) {
112128 throw new MetaBindInternalError ( 'inputFieldDeclaration is undefined, can not update metadata' ) ;
113129 }
@@ -118,31 +134,49 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
118134 throw new MetaBindInternalError ( 'bindTargetMetadataField or bindTargetFile is undefined, can not update metadata' ) ;
119135 }
120136
121- if ( this . valueQueue . length > 0 ) {
122- // console.log (this.valueQueue. at(-1))
123- await this . plugin . updateMetaData ( this . bindTargetMetadataField , this . valueQueue . at ( - 1 ) , this . bindTargetFile ) ;
124- this . valueQueue = [ ] ;
137+ if ( this . metadataValueUpdateQueue . length > 0 ) {
138+ await this . plugin . updateMetaData ( this . bindTargetMetadataField , this . metadataValueUpdateQueue . at ( - 1 ) , this . bindTargetFile ) ;
139+ } else {
140+ throw new MetaBindInternalError ( `cannot apply metadataValueUpdateQueue to inputField ${ this . uid } , metadataValueUpdateQueue is empty` ) ;
125141 }
126142 }
127143
128- async pushToValueQueue ( value : any ) : Promise < void > {
129- if ( this . inputFieldDeclaration ?. isBound ) {
130- this . valueQueue . push ( value ) ;
144+ async applyInputFieldValueUpdateQueue ( ) : Promise < void > {
145+ if ( ! this . inputFieldDeclaration ) {
146+ throw new MetaBindInternalError ( 'inputFieldDeclaration is undefined, can not update inputField' ) ;
131147 }
132- }
133-
134- updateValue ( value : any ) : void {
135148 if ( ! this . inputField ) {
136- throw new MetaBindInternalError ( 'inputField is undefined, can not update value ' ) ;
149+ throw new MetaBindInternalError ( 'inputField is undefined, can not update inputField ' ) ;
137150 }
138151
139- if ( value == null ) {
140- value = this . inputField . getDefaultValue ( ) ;
141- }
152+ if ( this . inputFieldValueUpdateQueue . length > 0 ) {
153+ let value = this . inputFieldValueUpdateQueue . at ( - 1 ) ;
154+
155+ if ( value == null ) {
156+ value = this . inputField . getDefaultValue ( ) ;
157+ }
142158
143- if ( ! this . inputField . isEqualValue ( value ) && this . valueQueue . length === 0 ) {
144159 Logger . logDebug ( `updating input field ${ this . uid } to` , value ) ;
145160 this . inputField . setValue ( value ) ;
161+ } else {
162+ throw new MetaBindInternalError ( `cannot apply inputFieldValueUpdateQueue to inputField ${ this . uid } , inputFieldValueUpdateQueue is empty` ) ;
163+ }
164+ }
165+
166+ cleanUpUpdateQueues ( ) : void {
167+ this . metadataValueUpdateQueue = [ ] ;
168+ this . inputFieldValueUpdateQueue = [ ] ;
169+ }
170+
171+ pushToMetadataValueUpdateQueue ( value : any ) : void {
172+ if ( this . inputFieldDeclaration ?. isBound ) {
173+ this . metadataValueUpdateQueue . push ( value ) ;
174+ }
175+ }
176+
177+ pushToInputFieldValueUpdateQueue ( value : any ) : void {
178+ if ( ! this . inputField ?. isEqualValue ( value ) ) {
179+ this . inputFieldValueUpdateQueue . push ( value ) ;
146180 }
147181 }
148182
0 commit comments