mirror of
https://github.com/zadam/trilium.git
synced 2025-01-16 03:58:53 +08: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
1 changed files with 27 additions and 7 deletions
|
@ -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 = [];
|
||||
|
||||
for (const noteId in noteTitles) {
|
||||
evaluateSimilarity(title, noteTitles[noteId], noteId, 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);
|
||||
|
|
Loading…
Reference in a new issue