mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
similar notes is async'd a bit to not block the event loop too much
This commit is contained in:
parent
494ec0b051
commit
9404e27cba
@ -364,17 +364,37 @@ function evaluateSimilarity(text1, text2, noteId, results) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function findSimilarNotes(title) {
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i % 200 === 0) {
|
||||||
|
await setImmediatePromise();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findSimilarNotes(title) {
|
||||||
const results = [];
|
const results = [];
|
||||||
|
|
||||||
for (const noteId in noteTitles) {
|
await evaluateSimilarityDict(title, noteTitles, results);
|
||||||
evaluateSimilarity(title, noteTitles[noteId], noteId, results);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (protectedSessionService.isProtectedSessionAvailable()) {
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
||||||
for (const noteId in protectedNoteTitles) {
|
await evaluateSimilarityDict(title, protectedNoteTitles, results);
|
||||||
evaluateSimilarity(title, protectedNoteTitles[noteId], noteId, results);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
results.sort((a, b) => a.coeff > b.coeff ? -1 : 1);
|
results.sort((a, b) => a.coeff > b.coeff ? -1 : 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user