trilium/src/public/javascripts/services/tree_cache.js

249 lines
7.6 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import Branch from "../entities/branch.js";
import NoteShort from "../entities/note_short.js";
2018-03-26 09:29:35 +08:00
import infoService from "./info.js";
2019-08-27 02:21:43 +08:00
import ws from "./ws.js";
import server from "./server.js";
/**
* TreeCache keeps a read only cache of note tree structure in frontend's memory.
*/
class TreeCache {
constructor() {
this.init();
}
load(noteRows, branchRows, relations) {
this.init();
this.addResp(noteRows, branchRows, relations);
}
init() {
2019-04-14 04:10:16 +08:00
/** @type {Object.<string, string>} */
this.parents = {};
2019-04-14 04:10:16 +08:00
/** @type {Object.<string, string>} */
this.children = {};
2019-04-14 04:10:16 +08:00
/** @type {Object.<string, string>} */
this.childParentToBranch = {};
/** @type {Object.<string, NoteShort>} */
this.notes = {};
/** @type {Object.<string, Branch>} */
this.branches = {};
}
addResp(noteRows, branchRows, relations) {
for (const noteRow of noteRows) {
const note = new NoteShort(this, noteRow);
this.notes[note.noteId] = note;
}
for (const branchRow of branchRows) {
const branch = new Branch(this, branchRow);
this.addBranch(branch);
}
for (const relation of relations) {
this.addBranchRelationship(relation.branchId, relation.childNoteId, relation.parentNoteId);
}
}
/**
* Reload children of given noteId.
*/
async reloadChildren(noteId) {
2019-04-14 04:10:16 +08:00
const resp = await server.post('tree/load', { noteIds: [noteId] });
for (const childNoteId of this.children[noteId] || []) {
this.parents[childNoteId] = this.parents[childNoteId].filter(p => p !== noteId);
const branchId = this.getBranchIdByChildParent(childNoteId, noteId);
delete this.branches[branchId];
delete this.childParentToBranch[childNoteId + '-' + noteId];
}
this.children[noteId] = [];
delete this.notes[noteId];
this.addResp(resp.notes, resp.branches, resp.relations);
}
/**
* Reloads parents of given noteId - useful when new note is created to make sure note is loaded
* in a correct order.
*/
async reloadParents(noteId) {
// to be able to find parents we need first to make sure it is actually loaded
await this.getNote(noteId);
for (const parentNoteId of this.parents[noteId] || []) {
await this.reloadChildren(parentNoteId);
}
// this is done to load the new parents for the noteId
await this.reloadChildren(noteId);
}
2019-04-14 04:10:16 +08:00
/** @return {Promise<NoteShort[]>} */
async getNotes(noteIds, silentNotFoundError = false) {
const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);
if (missingNoteIds.length > 0) {
const resp = await server.post('tree/load', { noteIds: missingNoteIds });
this.addResp(resp.notes, resp.branches, resp.relations);
for (const note of resp.notes) {
await this.reloadParents(note.noteId);
}
}
return noteIds.map(noteId => {
if (!this.notes[noteId] && !silentNotFoundError) {
2019-08-27 02:21:43 +08:00
ws.logError(`Can't find note "${noteId}"`);
2018-08-06 17:30:37 +08:00
2018-08-12 18:59:38 +08:00
return null;
}
else {
return this.notes[noteId];
}
2018-08-12 18:59:38 +08:00
}).filter(note => note !== null);
}
2019-04-14 04:10:16 +08:00
/** @return {Promise<boolean>} */
async noteExists(noteId) {
const notes = await this.getNotes([noteId], true);
return notes.length === 1;
}
/** @return {Promise<NoteShort>} */
async getNote(noteId) {
2018-05-27 04:16:34 +08:00
if (noteId === 'none') {
return null;
}
return (await this.getNotes([noteId]))[0];
}
addBranch(branch) {
this.branches[branch.branchId] = branch;
this.addBranchRelationship(branch.branchId, branch.noteId, branch.parentNoteId);
}
addBranchRelationship(branchId, childNoteId, parentNoteId) {
2018-05-27 04:16:34 +08:00
if (parentNoteId === 'none') { // applies only to root element
return;
}
this.childParentToBranch[childNoteId + '-' + parentNoteId] = branchId;
this.parents[childNoteId] = this.parents[childNoteId] || [];
if (!this.parents[childNoteId].includes(parentNoteId)) {
this.parents[childNoteId].push(parentNoteId);
}
this.children[parentNoteId] = this.children[parentNoteId] || [];
if (!this.children[parentNoteId].includes(childNoteId)) {
this.children[parentNoteId].push(childNoteId);
}
}
add(note, branch) {
this.notes[note.noteId] = note;
this.addBranch(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, resp.relations);
}
return branchIds.map(branchId => {
if (!this.branches[branchId]) {
throw new Error(`Can't find branch ${branchId}`);
}
else {
return this.branches[branchId];
}
});
}
/** @return Branch */
async getBranch(branchId) {
return (await this.getBranches([branchId]))[0];
}
/** @return Branch */
async getBranchByChildParent(childNoteId, parentNoteId) {
const branchId = this.getBranchIdByChildParent(childNoteId, parentNoteId);
return await this.getBranch(branchId);
}
getBranchIdByChildParent(childNoteId, parentNoteId) {
const key = childNoteId + '-' + parentNoteId;
const branchId = this.childParentToBranch[key];
if (!branchId) {
2018-03-26 09:29:35 +08:00
infoService.throwError("Cannot find branch for child-parent=" + key);
}
return branchId;
}
2018-03-27 10:11:45 +08:00
/* Move note from one parent to another. */
async moveNote(childNoteId, oldParentNoteId, newParentNoteId, beforeNoteId, afterNoteId) {
2018-03-27 10:11:45 +08:00
utils.assertArguments(childNoteId, oldParentNoteId, newParentNoteId);
if (oldParentNoteId === newParentNoteId) {
return;
}
const branchId = this.childParentToBranch[childNoteId + '-' + oldParentNoteId];
const branch = await this.getBranch(branchId);
branch.parentNoteId = newParentNoteId;
this.childParentToBranch[childNoteId + '-' + newParentNoteId] = branchId;
delete this.childParentToBranch[childNoteId + '-' + oldParentNoteId]; // this is correct because we know that oldParentId isn't same as newParentId
2018-03-27 10:11:45 +08:00
// remove old associations
this.parents[childNoteId] = this.parents[childNoteId].filter(p => p !== oldParentNoteId);
this.children[oldParentNoteId] = this.children[oldParentNoteId].filter(ch => ch !== childNoteId);
2018-03-27 10:11:45 +08:00
// add new associations
this.parents[childNoteId].push(newParentNoteId);
2018-03-27 10:11:45 +08:00
const children = this.children[newParentNoteId] = this.children[newParentNoteId] || []; // this might be first child
// we try to put the note into precise order which might be used again by lazy-loaded nodes
if (beforeNoteId && children.includes(beforeNoteId)) {
children.splice(children.indexOf(beforeNoteId), 0, childNoteId);
}
else if (afterNoteId && children.includes(afterNoteId)) {
children.splice(children.indexOf(afterNoteId) + 1, 0, childNoteId);
}
else {
children.push(childNoteId);
}
2018-03-27 10:11:45 +08:00
}
}
const treeCache = new TreeCache();
export default treeCache;