frontend sync through websocket

This commit is contained in:
zadam 2020-01-31 20:52:31 +01:00
parent 8bf794f13b
commit 05b51c0f68
5 changed files with 92 additions and 57 deletions

6
package-lock.json generated
View File

@ -8990,9 +8990,9 @@
}
},
"semver": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.1.1.tgz",
"integrity": "sha512-WfuG+fl6eh3eZ2qAf6goB7nhiCd7NPXhmyFxigB/TOkQyeLP8w8GsVehvtGNtnNmyboz4TgeK40B1Kbql/8c5A=="
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.1.2.tgz",
"integrity": "sha512-BJs9T/H8sEVHbeigqzIEo57Iu/3DG6c4QoqTfbQB3BPA4zgzAomh/Fk9E7QtjWQ8mx2dgA9YCfSF4y9k9bHNpQ=="
},
"semver-compare": {
"version": "1.0.0",

View File

@ -62,7 +62,7 @@
"rimraf": "3.0.1",
"sanitize-filename": "1.6.3",
"sax": "1.2.4",
"semver": "7.1.1",
"semver": "7.1.2",
"serve-favicon": "2.5.0",
"session-file-store": "1.4.0",
"simple-node-logger": "18.12.24",

View File

@ -13,9 +13,8 @@ class NoteShort {
/**
* @param {TreeCache} treeCache
* @param {Object.<string, Object>} row
* @param {Branch[]} branches - all relevant branches, i.e. where this note is either child or parent
*/
constructor(treeCache, row, branches) {
constructor(treeCache, row) {
this.treeCache = treeCache;
/** @type {string[]} */
@ -35,10 +34,10 @@ class NoteShort {
/** @type {Object.<string, string>} */
this.childToBranch = {};
this.update(row, branches);
this.update(row);
}
update(row, branches = []) {
update(row) {
/** @param {string} */
this.noteId = row.noteId;
/** @param {string} */
@ -53,20 +52,6 @@ class NoteShort {
this.mime = row.mime;
/** @param {boolean} */
this.isDeleted = row.isDeleted;
for (const branch of branches) {
if (this.noteId === branch.noteId) {
this.parents.push(branch.parentNoteId);
this.parentToBranch[branch.parentNoteId] = branch.branchId;
}
else if (this.noteId === branch.parentNoteId) {
this.children.push(branch.noteId);
this.childToBranch[branch.noteId] = branch.branchId;
}
else {
throw new Error(`Unknown branch ${branch.branchId} for note ${this.noteId}`);
}
}
}
addParent(parentNoteId, branchId) {

View File

@ -35,18 +35,6 @@ class TreeCache {
}
addResp(noteRows, branchRows, attributeRows) {
const branchesByNotes = {};
for (const branchRow of branchRows) {
const branch = new Branch(this, branchRow);
branchesByNotes[branch.noteId] = branchesByNotes[branch.noteId] || [];
branchesByNotes[branch.noteId].push(branch);
branchesByNotes[branch.parentNoteId] = branchesByNotes[branch.parentNoteId] || [];
branchesByNotes[branch.parentNoteId].push(branch);
}
for (const noteRow of noteRows) {
const {noteId} = noteRow;
@ -76,28 +64,26 @@ class TreeCache {
}
}
for (const branch of branchesByNotes[noteId] || []) { // can be empty for deleted notes
this.branches[branch.branchId] = branch;
}
const note = new NoteShort(this, noteRow, branchesByNotes[noteId] || []);
const note = new NoteShort(this, noteRow);
this.notes[note.noteId] = note;
}
for (const childNoteId of note.children) {
const childNote = this.notes[childNoteId];
for (const branchRow of branchRows) {
const branch = new Branch(this, branchRow);
if (childNote) {
childNote.addParent(noteId, note.childToBranch[childNoteId]);
}
this.branches[branch.branchId] = branch;
const childNote = this.notes[branch.noteId];
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
}
for (const parentNoteId of note.parents) {
const parentNote = this.notes[parentNoteId];
const parentNote = this.notes[branch.parentNoteId];
if (parentNote) {
parentNote.addChild(noteId, note.parentToBranch[parentNoteId]);
}
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId);
}
}
@ -244,11 +230,50 @@ class TreeCache {
});
syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => {
const branch = this.branches[sync.entityId];
let branch = this.branches[sync.entityId];
const childNote = this.notes[sync.entity.noteId];
const parentNote = this.notes[sync.entity.parentNoteId];
if (branch) {
branch.update(sync.entity);
loadResults.addBranch(sync.entityId, sync.sourceId);
if (sync.entity.isDeleted) {
if (childNote) {
childNote.parents = childNote.parents.filter(parentNoteId => parentNoteId !== sync.entity.parentNoteId);
delete childNote.parentToBranch[sync.entity.parentNoteId];
}
if (parentNote) {
parentNote.children = parentNote.children.filter(childNoteId => childNoteId !== sync.entity.noteId);
delete parentNote.childToBranch[sync.entity.noteId];
}
}
else {
branch.update(sync.entity);
loadResults.addBranch(sync.entityId, sync.sourceId);
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
}
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId);
}
}
}
else if (!sync.entity.isDeleted) {
if (childNote || parentNote) {
branch = new Branch(this, sync.entity);
this.branches[branch.branchId] = branch;
loadResults.addBranch(sync.entityId, sync.sourceId);
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
}
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId);
}
}
}
});
@ -266,11 +291,40 @@ class TreeCache {
// missing reloading the relation target note
syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => {
const attribute = this.attributes[sync.entityId];
let attribute = this.attributes[sync.entityId];
const sourceNote = this.notes[sync.entity.noteId];
const targetNote = sync.entity.type === 'relation' && this.notes[sync.entity.value];
if (attribute) {
attribute.update(sync.entity);
loadResults.addAttribute(sync.entityId, sync.sourceId);
if (sync.entity.isDeleted) {
if (sourceNote) {
sourceNote.attributes = sourceNote.attributes.filter(attributeId => attributeId !== attribute.attributeId);
}
if (targetNote) {
targetNote.targetRelations = targetNote.targetRelations.filter(attributeId => attributeId !== attribute.value);
}
}
}
else if (!sync.entity.isDeleted) {
if (sourceNote || targetNote) {
attribute = new Attribute(this, sync.entity);
this.attributes[attribute.attributeId] = attribute;
loadResults.addAttribute(sync.entityId, sync.sourceId);
if (sourceNote && !sourceNote.attributes.includes(attribute.attributeId)) {
sourceNote.attributes.push(attribute.attributeId);
}
if (targetNote && !targetNote.attributes.includes(attribute.attributeId)) {
targetNote.attributes.push(attribute.attributeId);
}
}
}
});

View File

@ -89,11 +89,7 @@ class AttributesWidget extends StandardWidget {
}
entitiesReloadedListener({loadResults}) {
console.log("CHECK ATTRS", loadResults.getAttributes());
if (loadResults.getAttributes().find(attr => attr.noteId === this.noteId)) {
console.log("JAAAJ");
this.refresh();
}
}