frontend attribute cache refactoring WIP

This commit is contained in:
zadam
2020-01-25 11:52:45 +01:00
parent 52a907651e
commit 60c908cd63
11 changed files with 102 additions and 116 deletions

View File

@@ -2,6 +2,7 @@ import Branch from "../entities/branch.js";
import NoteShort from "../entities/note_short.js";
import ws from "./ws.js";
import server from "./server.js";
import Attribute from "../entities/attribute.js";
/**
* TreeCache keeps a read only cache of note tree structure in frontend's memory.
@@ -22,15 +23,18 @@ class TreeCache {
/** @type {Object.<string, Branch>} */
this.branches = {};
/** @type {Object.<string, Attribute>} */
this.attributes = {};
}
load(noteRows, branchRows) {
load(noteRows, branchRows, attributeRows) {
this.init();
this.addResp(noteRows, branchRows);
this.addResp(noteRows, branchRows, attributeRows);
}
addResp(noteRows, branchRows) {
addResp(noteRows, branchRows, attributeRows) {
const branchesByNotes = {};
for (const branchRow of branchRows) {
@@ -96,6 +100,28 @@ class TreeCache {
}
}
}
for (const attributeRow of attributeRows) {
const {attributeId} = attributeRow;
this.attributes[attributeId] = new Attribute(this, attributeRow);
const note = this.notes[attributeRow.noteId];
if (!note.attributes.includes(attributeId)) {
note.attributes.push(attributeId);
}
if (attributeRow.type === 'relation') {
const targetNote = this.notes[attributeRow.value];
if (targetNote) {
if (!note.targetRelations.includes(attributeId)) {
note.targetRelations.push(attributeId);
}
}
}
}
}
async reloadNotes(noteIds) {