feat(text-snippets): better reaction to removing templates

This commit is contained in:
Elian Doran 2025-06-17 17:19:50 +03:00
parent 3e40a35c19
commit 4f9bd970af
No known key found for this signature in database

View File

@ -46,12 +46,11 @@ async function handleContentUpdate(affectedNoteIds: string[]) {
const templateNoteIds = new Set(templateCache.keys());
const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds);
console.log("Got ", affectedTemplateNoteIds);
await froca.getNotes(affectedNoteIds);
let fullReloadNeeded = false;
for (const affectedTemplateNoteId of affectedTemplateNoteIds) {
try {
const template = await froca.getNote(affectedTemplateNoteId);
if (!template) {
console.warn("Unable to obtain template with ID ", affectedTemplateNoteId);
@ -68,6 +67,10 @@ async function handleContentUpdate(affectedNoteIds: string[]) {
title: template.title,
content: await template.getContent()
});
} catch (e) {
// If a note was not found while updating the cache, it means we need to do a full reload.
fullReloadNeeded = true;
}
}
if (fullReloadNeeded) {
@ -77,14 +80,15 @@ async function handleContentUpdate(affectedNoteIds: string[]) {
export function updateTemplateCache(loadResults: LoadResults): boolean {
const affectedNoteIds = loadResults.getNoteIds();
if (affectedNoteIds.length > 0) {
debouncedHandleContentUpdate(affectedNoteIds);
}
// React to creation or deletion of text snippets.
if (loadResults.getAttributeRows().find((attr) =>
attr.type === "label" &&
attr.name === "textSnippet")) {
handleFullReload();
} else if (affectedNoteIds.length > 0) {
// Update content and titles if one of the template notes were updated.
debouncedHandleContentUpdate(affectedNoteIds);
}
return false;