Skip to content

Commit 4a357f2

Browse files
committed
Update sync algorithm #23
1 parent fb177ad commit 4a357f2

File tree

6 files changed

+129
-53
lines changed

6 files changed

+129
-53
lines changed

exampleVault/.obsidian/plugins/obsidian-meta-bind-plugin/main.js

Lines changed: 56 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exampleVault/examples.md renamed to exampleVault/examples fg.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ rating: 31
33
title: test title test test
44
completed: false
55
toggle1: false
6-
slider1: 7
6+
slider1: 8
77
slider2: 4
88
text1: Test text
99
text_area1: Test test
1010
date1: 2022-05-28
11-
select: option b
11+
select: option c
1212
multi-select:
1313
- option a
1414
- option c

exampleVault/other note.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
---
2-
title: This is an example of how the meta bind plugin works
3-
select: option c
2+
title: >-
3+
This is an example of how the meta bind
4+
abababababababababababababababababaabbababababababababababababababababab
5+
6+
asdasdkahjbd
7+
adjsnbf o
8+
asdoasdasdfm
9+
10+
asodfnsopdnfosndflskxn slsdf
11+
12+
sdf sdjfpsdjfpsodjfpsodjfspodjfspodfjspodfspodfjspdofjspdofpsodfj
13+
select: option a
414
multi-select:
515
- option a
6-
- option b
7-
date: Saturday, October 13th 2029
16+
- option c
17+
date: Thursday, February 17th 2022
818
time: 19:20
919
---

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/InputFieldMarkdownRenderChild.ts

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class MetaBindPlugin extends Plugin {
9999
if (metadata === undefined) {
100100
metadata = await this.getMetadataFromFileCache(cache.frontmatter);
101101
}
102-
activeMarkdownInputField.updateValue(metadata[activeMarkdownInputField.bindTargetMetadataField]);
102+
activeMarkdownInputField.pushToInputFieldValueUpdateQueue(metadata[activeMarkdownInputField.bindTargetMetadataField]);
103103
}
104104
}
105105
}

0 commit comments

Comments
 (0)