diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index 3adafda84..370fbb89a 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -854,7 +854,7 @@ async function reloadNotes(noteIds, activateNotePath = null) { const node = await getNodeFromPath(activateNotePath); if (node) { - await node.setActive(true, {noEvents: true}); // this node has been already active so no need to fire events again + await node.setActive(true); } } } diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js index a8997c264..d183a6410 100644 --- a/src/public/javascripts/services/tree_builder.js +++ b/src/public/javascripts/services/tree_builder.js @@ -115,21 +115,22 @@ async function prepareSearchBranch(note) { // force to load all the notes at once instead of one by one await treeCache.getNotes(results.map(res => res.noteId)); - for (const result of results) { - const origBranch = treeCache.getBranch(result.branchId); + const {notes, branches} = await server.post('tree/load', { noteIds: [note.noteId] }); - const branch = new Branch(treeCache, { - branchId: "virt" + utils.randomString(10), - noteId: result.noteId, - parentNoteId: note.noteId, - prefix: origBranch.prefix, - virtual: true - }); + results.forEach((result, index) => branches.push({ + branchId: "virt" + utils.randomString(10), + noteId: result.noteId, + parentNoteId: note.noteId, + prefix: treeCache.getBranch(result.branchId).prefix, + notePosition: (index + 1) * 10 + })); - treeCache.addBranch(branch); - } + treeCache.addResp(notes, branches); - return await prepareRealBranch(note); + // note in cache changed + const newNote = await treeCache.getNote(note.noteId); + + return await prepareRealBranch(newNote); } async function getExtraClasses(note) { diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index 3984c0448..8371c2291 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -5,6 +5,11 @@ import server from "./server.js"; /** * TreeCache keeps a read only cache of note tree structure in frontend's memory. + * - notes are loaded lazily when unknown noteId is requested + * - when note is loaded, all its parent and child branches are loaded as well. For a branch to be used, it's not must be loaded before + * - deleted notes are present in the cache as well, but they don't have any branches. As a result check for deleted branch is done by presence check - if the branch is not there even though the corresponding note has been loaded, we can infer it is deleted. + * + * Note and branch deletions are corner cases and usually not needed. */ class TreeCache { constructor() { @@ -50,12 +55,9 @@ class TreeCache { if (childNote) { childNote.parents = childNote.parents.filter(p => p !== noteId); - const branchId = childNote.parentToBranch[noteId]; - - if (branchId in this.branches) { - delete this.branches[branchId]; - } + console.log("Cleaning up", childNote.parentToBranch[noteId]); + delete this.branches[childNote.parentToBranch[noteId]]; delete childNote.parentToBranch[noteId]; } } @@ -66,19 +68,18 @@ class TreeCache { if (parentNote) { parentNote.children = parentNote.children.filter(p => p !== noteId); - const branchId = parentNote.childToBranch[noteId]; - - if (branchId in this.branches) { - delete this.branches[branchId]; - } - + delete this.branches[parentNote.childToBranch[noteId]]; delete parentNote.childToBranch[noteId]; } } } for (const branch of branchesByNotes[noteId] || []) { // can be empty for deleted notes - this.addBranch(branch); + if (noteId === '2Ndfjyv3EbEQ') { + console.log("Adding", branch.branchId); + } + + this.branches[branch.branchId] = branch; } const note = new NoteShort(this, noteRow, branchesByNotes[noteId] || []); @@ -147,10 +148,6 @@ class TreeCache { return (await this.getNotes([noteId], silentNotFoundError))[0]; } - addBranch(branch) { - this.branches[branch.branchId] = branch; - } - getBranches(branchIds) { return branchIds .map(branchId => this.getBranch(branchId))