Skip to content

Build your personal knowledge base with Trilium Notes

frontend sync through websocket

zadam05b51c0f689a
diff --git a/package-lock.json b/package-lock.json
index 3c4253fc41..274ab3f297 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -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",
diff --git a/package.json b/package.json
index 45191c5bc3..fceb08aae6 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.js
index c6f295e6ab..da3ae2f48c 100644
--- a/src/public/javascripts/entities/note_short.js
+++ b/src/public/javascripts/entities/note_short.js
@@ -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) {
diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js
index 3c87764738..404c52775c 100644
--- a/src/public/javascripts/services/tree_cache.js
+++ b/src/public/javascripts/services/tree_cache.js
@@ -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);
+ }
+ }
}
});
diff --git a/src/public/javascripts/widgets/attributes.js b/src/public/javascripts/widgets/attributes.js
index 2cf8fdd8ee..9812eedade 100644
--- a/src/public/javascripts/widgets/attributes.js
+++ b/src/public/javascripts/widgets/attributes.js
@@ -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();
}
}