refactor(text-snippets): use a map instead of an object

This commit is contained in:
Elian Doran 2025-06-17 16:48:30 +03:00
parent 421e125882
commit 9f82e0a6d6
No known key found for this signature in database

View File

@ -3,7 +3,7 @@ import type LoadResults from "../../../services/load_results.js";
import search from "../../../services/search.js"; import search from "../../../services/search.js";
import type { TemplateDefinition } from "@triliumnext/ckeditor5"; import type { TemplateDefinition } from "@triliumnext/ckeditor5";
let templateCache: Record<string, string> = {}; let templateCache: Map<string, string | undefined> = new Map();
/** /**
* Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration. * Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration.
@ -15,11 +15,11 @@ export default async function getTemplates() {
const snippets = await search.searchForNotes("#textSnippet"); const snippets = await search.searchForNotes("#textSnippet");
const definitions: TemplateDefinition[] = []; const definitions: TemplateDefinition[] = [];
for (const snippet of snippets) { for (const snippet of snippets) {
templateCache[snippet.noteId] = await (snippet.getContent()) ?? ""; templateCache.set(snippet.noteId, await snippet.getContent());
definitions.push({ definitions.push({
title: snippet.title, title: snippet.title,
data: () => templateCache[snippet.noteId] data: () => templateCache.get(snippet.noteId) ?? ""
}) })
} }
return definitions; return definitions;
@ -27,7 +27,7 @@ export default async function getTemplates() {
async function handleUpdate(affectedNoteIds: string[]) { async function handleUpdate(affectedNoteIds: string[]) {
const updatedNoteIds = new Set(affectedNoteIds); const updatedNoteIds = new Set(affectedNoteIds);
const templateNoteIds = new Set(Object.keys(templateCache)); const templateNoteIds = new Set(templateCache.keys());
const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds); const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds);
console.log("Got ", affectedTemplateNoteIds); console.log("Got ", affectedTemplateNoteIds);
@ -41,7 +41,7 @@ async function handleUpdate(affectedNoteIds: string[]) {
continue; continue;
} }
templateCache[affectedTemplateNoteId] = await template.getContent() ?? ""; templateCache.set(affectedTemplateNoteId, await template.getContent());
} }
} }