Skip to content

Commit 35cbd62

Browse files
stevenwcarterdsebastien
authored andcommitted
feat: Enable updates to all files, not just recently modified
The plugin was assuming that only files with recent updates would need their serialized queries updated. However, sometimes files that didn't previously match a query would match a query after an update. This update also corrects the logic around the recently updated files, so that files are only tracked against the last time the update ran, not against their mtime. Files that haven't been modified in a while might still end up having updates due to a file now matching the query.
1 parent 4f21c2a commit 35cbd62

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

apps/plugin/src/app/plugin.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ export class DataviewSerializerPlugin extends Plugin {
5252
true
5353
);
5454

55+
scheduleFullUpdate = debounce(
56+
this.processAllFiles.bind(this),
57+
MINIMUM_MS_BETWEEN_EVENTS * 20,
58+
true
59+
);
60+
61+
async processAllFiles(): Promise<void> {
62+
this.app.vault.getMarkdownFiles().forEach((file) => {
63+
this.processFile(file);
64+
});
65+
}
5566
/**
5667
* Process all the identified recently updated files
5768
*/
@@ -172,20 +183,23 @@ export class DataviewSerializerPlugin extends Plugin {
172183
this.app.vault.on('create', (file) => {
173184
this.recentlyUpdatedFiles.add(file);
174185
this.scheduleUpdate();
186+
this.scheduleFullUpdate();
175187
})
176188
);
177189

178190
this.registerEvent(
179191
this.app.vault.on('rename', (file) => {
180192
this.recentlyUpdatedFiles.add(file);
181193
this.scheduleUpdate();
194+
this.scheduleFullUpdate();
182195
})
183196
);
184197

185198
this.registerEvent(
186199
this.app.vault.on('modify', (file) => {
187200
this.recentlyUpdatedFiles.add(file);
188201
this.scheduleUpdate();
202+
this.scheduleFullUpdate();
189203
})
190204
);
191205
});
@@ -207,14 +221,16 @@ export class DataviewSerializerPlugin extends Plugin {
207221
try {
208222
//log(`Processing file: ${file.path}`, 'debug');
209223

210-
const text = await this.app.vault.cachedRead(file);
211-
const foundQueries: string[] = findQueries(text);
224+
const cachedText = await this.app.vault.cachedRead(file);
225+
const foundQueries: string[] = findQueries(cachedText);
212226

213227
if (foundQueries.length === 0) {
214228
// No queries to serialize found in the file
215229
return;
216230
}
217231

232+
const text = await this.app.vault.read(file);
233+
218234
// Process the modified file
219235
let updatedText = `${text}`; // To ensure we have access to replaceAll...
220236

@@ -261,7 +277,7 @@ export class DataviewSerializerPlugin extends Plugin {
261277
}
262278

263279
// Keep track of the last time this file was updated to avoid modification loops
264-
const nextPossibleUpdateTimeForFile = add(new Date(file.stat.mtime), {
280+
const nextPossibleUpdateTimeForFile = add(new Date(), {
265281
seconds: MINIMUM_SECONDS_BETWEEN_UPDATES,
266282
});
267283
this.nextPossibleUpdates.set(file.path, nextPossibleUpdateTimeForFile);
@@ -308,7 +324,7 @@ export class DataviewSerializerPlugin extends Plugin {
308324
file.path
309325
)!;
310326

311-
if (isBefore(file.stat.mtime, nextPossibleUpdateForFile)) {
327+
if (isBefore(new Date(), nextPossibleUpdateForFile)) {
312328
log('File has been updated recently. Ignoring', 'debug', file.path);
313329
return true;
314330
} else {

0 commit comments

Comments
 (0)