Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
similar notes is async'd a bit to not block the event loop too much
zadam9404e27cbacf
diff --git a/src/services/note_cache.js b/src/services/note_cache.jsindex 3c628e0b80..5368fb09e7 100644--- a/src/services/note_cache.js+++ b/src/services/note_cache.js@@ -364,17 +364,37 @@ function evaluateSimilarity(text1, text2, noteId, results) {}}-function findSimilarNotes(title) {- const results = [];+/**+ * Point of this is to break up long running sync process to avoid blocking+ * see https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/+ */+function setImmediatePromise() {+ return new Promise((resolve) => {+ setImmediate(() => resolve());+ });+}++async function evaluateSimilarityDict(title, dict, results) {+ let i = 0;++ for (const noteId in dict) {+ evaluateSimilarity(title, dict[noteId], noteId, results);- for (const noteId in noteTitles) {- evaluateSimilarity(title, noteTitles[noteId], noteId, results);+ i++;++ if (i % 200 === 0) {+ await setImmediatePromise();+ }}+}++async function findSimilarNotes(title) {+ const results = [];++ await evaluateSimilarityDict(title, noteTitles, results);if (protectedSessionService.isProtectedSessionAvailable()) {- for (const noteId in protectedNoteTitles) {- evaluateSimilarity(title, protectedNoteTitles[noteId], noteId, results);- }+ await evaluateSimilarityDict(title, protectedNoteTitles, results);}results.sort((a, b) => a.coeff > b.coeff ? -1 : 1);