diff --git a/src/public/javascripts/dialogs/branch_prefix.js b/src/public/javascripts/dialogs/branch_prefix.js index 4c31c135a..4d67e5123 100644 --- a/src/public/javascripts/dialogs/branch_prefix.js +++ b/src/public/javascripts/dialogs/branch_prefix.js @@ -20,7 +20,7 @@ export async function showDialog(node) { $dialog.modal(); branchId = node.data.branchId; - const branch = await treeCache.getBranch(branchId); + const branch = treeCache.getBranch(branchId); $treePrefixInput.val(branch.prefix); diff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.js index b737c0002..ccae6c0fb 100644 --- a/src/public/javascripts/entities/note_short.js +++ b/src/public/javascripts/entities/note_short.js @@ -127,7 +127,7 @@ class NoteShort { async getChildBranches() { const branchIds = Object.values(this.childToBranch); - return await this.treeCache.getBranches(branchIds); + return this.treeCache.getBranches(branchIds); } /** @returns {string[]} */ diff --git a/src/public/javascripts/mobile.js b/src/public/javascripts/mobile.js index 8e4255fca..d632cecb3 100644 --- a/src/public/javascripts/mobile.js +++ b/src/public/javascripts/mobile.js @@ -91,7 +91,7 @@ async function showTree() { $detail.on("click", ".note-menu-button", async e => { const node = treeService.getActiveNode(); - const branch = await treeCache.getBranch(node.data.branchId); + const branch = treeCache.getBranch(node.data.branchId); const note = await treeCache.getNote(node.data.noteId); const parentNote = await treeCache.getNote(branch.parentNoteId); const isNotRoot = note.noteId !== 'root'; diff --git a/src/public/javascripts/services/cloning.js b/src/public/javascripts/services/cloning.js index b801e70b5..5088d1cad 100644 --- a/src/public/javascripts/services/cloning.js +++ b/src/public/javascripts/services/cloning.js @@ -24,7 +24,7 @@ async function cloneNoteAfter(noteId, afterBranchId) { return; } - const afterBranch = await treeCache.getBranch(afterBranchId); + const afterBranch = treeCache.getBranch(afterBranchId); await treeService.reloadNotes([noteId, afterBranch.parentNoteId]); } diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index 6f29f44de..c6ce1ec67 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -48,7 +48,7 @@ function getActiveNode() { async function getNodesByBranchId(branchId) { utils.assertArguments(branchId); - const branch = await treeCache.getBranch(branchId); + const branch = treeCache.getBranch(branchId); return getNodesByNoteId(branch.noteId).filter(node => node.data.branchId === branchId); } @@ -64,7 +64,7 @@ function getNodesByNoteId(noteId) { async function setPrefix(branchId, prefix) { utils.assertArguments(branchId); - const branch = await treeCache.getBranch(branchId); + const branch = treeCache.getBranch(branchId); branch.prefix = prefix; @@ -75,7 +75,7 @@ async function setPrefix(branchId, prefix) { async function setNodeTitleWithPrefix(node) { const noteTitle = await treeUtils.getNoteTitle(node.data.noteId); - const branch = await treeCache.getBranch(node.data.branchId); + const branch = treeCache.getBranch(node.data.branchId); const prefix = branch.prefix; @@ -671,7 +671,7 @@ async function createNote(node, parentNoteId, target, extraOptions = {}) { noteDetailService.addDetailLoadedListener(note.noteId, noteDetailService.focusAndSelectTitle); const noteEntity = await treeCache.getNote(note.noteId); - const branchEntity = await treeCache.getBranch(branch.branchId); + const branchEntity = treeCache.getBranch(branch.branchId); let newNode = { title: newNoteName, diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js index 0480b480d..a8997c264 100644 --- a/src/public/javascripts/services/tree_builder.js +++ b/src/public/javascripts/services/tree_builder.js @@ -11,7 +11,7 @@ async function prepareTree() { let hoistedBranch; if (hoistedNoteId === 'root') { - hoistedBranch = await treeCache.getBranch('root'); + hoistedBranch = treeCache.getBranch('root'); } else { const hoistedNote = await treeCache.getNote(hoistedNoteId); @@ -116,7 +116,7 @@ async function prepareSearchBranch(note) { await treeCache.getNotes(results.map(res => res.noteId)); for (const result of results) { - const origBranch = await treeCache.getBranch(result.branchId); + const origBranch = treeCache.getBranch(result.branchId); const branch = new Branch(treeCache, { branchId: "virt" + utils.randomString(10), diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index 8da92f48f..ed905fd9a 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -1,4 +1,3 @@ -import utils from "./utils.js"; import Branch from "../entities/branch.js"; import NoteShort from "../entities/note_short.js"; import ws from "./ws.js"; @@ -151,28 +150,19 @@ class TreeCache { this.branches[branch.branchId] = branch; } - async getBranches(branchIds) { - const missingBranchIds = branchIds.filter(branchId => this.branches[branchId] === undefined); - - if (missingBranchIds.length > 0) { - const resp = await server.post('tree/load', { branchIds: branchIds }); - - this.addResp(resp.notes, resp.branches); - } - - return branchIds.map(branchId => { - if (!this.branches[branchId]) { - throw new Error(`Can't find branch ${branchId}`); - } - else { - return this.branches[branchId]; - } - }); + getBranches(branchIds) { + return branchIds + .map(branchId => this.getBranch(branchId)) + .filter(b => b !== null); } - /** @return Branch */ - async getBranch(branchId) { - return (await this.getBranches([branchId]))[0]; + /** @return {Branch} */ + getBranch(branchId) { + if (!(branchId in this.branches)) { + console.error(`Not existing branch ${branchId}`); + } + + return this.branches[branchId]; } } diff --git a/src/public/javascripts/services/tree_context_menu.js b/src/public/javascripts/services/tree_context_menu.js index bb2e6317d..61075d3bc 100644 --- a/src/public/javascripts/services/tree_context_menu.js +++ b/src/public/javascripts/services/tree_context_menu.js @@ -26,7 +26,7 @@ class TreeContextMenu { } async getContextMenuItems() { - const branch = await treeCache.getBranch(this.node.data.branchId); + const branch = treeCache.getBranch(this.node.data.branchId); const note = await treeCache.getNote(this.node.data.noteId); const parentNote = await treeCache.getNote(branch.parentNoteId); const isNotRoot = note.noteId !== 'root'; @@ -156,7 +156,7 @@ class TreeContextMenu { hoistedNoteService.unhoist(); } else if (cmd === "duplicateNote") { - const branch = await treeCache.getBranch(this.node.data.branchId); + const branch = treeCache.getBranch(this.node.data.branchId); treeService.duplicateNote(this.node.data.noteId, branch.parentNoteId); } diff --git a/src/public/javascripts/services/tree_utils.js b/src/public/javascripts/services/tree_utils.js index 79cf8b203..df396af1c 100644 --- a/src/public/javascripts/services/tree_utils.js +++ b/src/public/javascripts/services/tree_utils.js @@ -62,7 +62,7 @@ async function getNoteTitle(noteId, parentNoteId = null) { const branchId = note.parentToBranch[parentNoteId]; if (branchId) { - const branch = await treeCache.getBranch(branchId); + const branch = treeCache.getBranch(branchId); if (branch && branch.prefix) { title = branch.prefix + ' - ' + title; diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index afa6a33aa..3b092e5ec 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -99,15 +99,7 @@ async function getTree() { } async function load(req) { - let noteIds = req.body.noteIds; - const branchIds = req.body.branchIds; - - if (branchIds && branchIds.length > 0) { - noteIds = (await sql.getManyRows(`SELECT noteId FROM branches WHERE isDeleted = 0 AND branchId IN(???)`, branchIds)) - .map(note => note.noteId); - } - - return await getNotesAndBranches(noteIds); + return await getNotesAndBranches(req.body.noteIds); } module.exports = {