From 6edc6e28258cc023023128dd5552bcd5b4963dcf Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 18 Mar 2021 21:05:43 +0100 Subject: [PATCH] load search result only on explicit action (expand node, execute search), #1759 --- src/public/app/services/tree_cache.js | 70 ++++++++++--------- src/public/app/widgets/note_tree.js | 2 +- .../search_definition.js | 2 +- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/public/app/services/tree_cache.js b/src/public/app/services/tree_cache.js index 2614f23ef..4b9e2f7f5 100644 --- a/src/public/app/services/tree_cache.js +++ b/src/public/app/services/tree_cache.js @@ -164,43 +164,47 @@ class TreeCache { this.addResp(resp); - for (const note of resp.notes) { - if (note.type === 'search') { - const searchResultNoteIds = await server.get('search-note/' + note.noteId); + appContext.triggerEvent('notesReloaded', {noteIds}); + } - if (!Array.isArray(searchResultNoteIds)) { - throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`); - } + async loadSearchNote(noteId) { + const note = await this.getNote(noteId); - // reset all the virtual branches from old search results - if (note.noteId in treeCache.notes) { - treeCache.notes[note.noteId].children = []; - treeCache.notes[note.noteId].childToBranch = {}; - } - - const branches = resp.branches.filter(b => b.noteId === note.noteId || b.parentNoteId === note.noteId); - - searchResultNoteIds.forEach((resultNoteId, index) => branches.push({ - // branchId should be repeatable since sometimes we reload some notes without rerendering the tree - branchId: "virt-" + note.noteId + '-' + resultNoteId, - noteId: resultNoteId, - parentNoteId: note.noteId, - notePosition: (index + 1) * 10, - fromSearchNote: true - })); - - // update this note with standard (parent) branches + virtual (children) branches - this.addResp({ - notes: [note], - branches, - attributes: [] - }); - - treeCache.notes[note.noteId].searchResultsLoaded = true; - } + if (!note || note.type !== 'search') { + return; } - appContext.triggerEvent('notesReloaded', {noteIds}); + const searchResultNoteIds = await server.get('search-note/' + note.noteId); + + if (!Array.isArray(searchResultNoteIds)) { + throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`); + } + + // reset all the virtual branches from old search results + if (note.noteId in treeCache.notes) { + treeCache.notes[note.noteId].children = []; + treeCache.notes[note.noteId].childToBranch = {}; + } + + const branches = [...note.getBranches(), ...note.getChildBranches()]; + + searchResultNoteIds.forEach((resultNoteId, index) => branches.push({ + // branchId should be repeatable since sometimes we reload some notes without rerendering the tree + branchId: "virt-" + note.noteId + '-' + resultNoteId, + noteId: resultNoteId, + parentNoteId: note.noteId, + notePosition: (index + 1) * 10, + fromSearchNote: true + })); + + // update this note with standard (parent) branches + virtual (children) branches + this.addResp({ + notes: [note], + branches, + attributes: [] + }); + + treeCache.notes[note.noteId].searchResultsLoaded = true; } /** @return {NoteShort[]} */ diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js index 082ca86ec..a7c4cf4bd 100644 --- a/src/public/app/widgets/note_tree.js +++ b/src/public/app/widgets/note_tree.js @@ -472,7 +472,7 @@ export default class NoteTreeWidget extends TabAwareWidget { return; } - data.result = treeCache.reloadNotes([noteId]).then(() => { + data.result = treeCache.loadSearchNote(noteId).then(() => { const note = treeCache.getNoteFromCache(noteId); let childNoteIds = note.getChildNoteIds(); diff --git a/src/public/app/widgets/type_property_widgets/search_definition.js b/src/public/app/widgets/type_property_widgets/search_definition.js index d294c901b..4b061741c 100644 --- a/src/public/app/widgets/type_property_widgets/search_definition.js +++ b/src/public/app/widgets/type_property_widgets/search_definition.js @@ -265,7 +265,7 @@ export default class SearchDefinitionWidget extends TabAwareWidget { async refreshResultsCommand() { try { - await treeCache.reloadNotes([this.noteId]); + await treeCache.loadSearchNote(this.noteId); } catch (e) { toastService.showError(e.message);