|
|
|
|
@ -1,6 +1,10 @@
|
|
|
|
|
import utils from './utils.js';
|
|
|
|
|
import toastService from "./toast.js";
|
|
|
|
|
import server from "./server.js";
|
|
|
|
|
import LoadResults from "./load_results.js";
|
|
|
|
|
import Branch from "../entities/branch.js";
|
|
|
|
|
import Attribute from "../entities/attribute.js";
|
|
|
|
|
import options from "./options.js";
|
|
|
|
|
|
|
|
|
|
const $outstandingSyncsCount = $("#outstanding-syncs-count");
|
|
|
|
|
|
|
|
|
|
@ -125,9 +129,7 @@ async function consumeSyncData() {
|
|
|
|
|
syncDataQueue = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const treeCache = (await import("./tree_cache.js")).default;
|
|
|
|
|
|
|
|
|
|
await treeCache.processSyncRows(allSyncData);
|
|
|
|
|
await processSyncRows(allSyncData);
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
logError(`Encountered error ${e.message}: ${e.stack}, reloading frontend.`);
|
|
|
|
|
@ -197,6 +199,137 @@ subscribeToMessages(message => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function processSyncRows(syncRows) {
|
|
|
|
|
const loadResults = new LoadResults(this);
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'notes').forEach(sync => {
|
|
|
|
|
const note = this.notes[sync.entityId];
|
|
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
|
note.update(sync.entity);
|
|
|
|
|
loadResults.addNote(sync.entityId, sync.sourceId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => {
|
|
|
|
|
let branch = this.branches[sync.entityId];
|
|
|
|
|
const childNote = this.notes[sync.entity.noteId];
|
|
|
|
|
const parentNote = this.notes[sync.entity.parentNoteId];
|
|
|
|
|
|
|
|
|
|
if (branch) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'note_reordering').forEach(sync => {
|
|
|
|
|
for (const branchId in sync.positions) {
|
|
|
|
|
const branch = this.branches[branchId];
|
|
|
|
|
|
|
|
|
|
if (branch) {
|
|
|
|
|
branch.notePosition = sync.positions[branchId];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadResults.addNoteReordering(sync.entityId, sync.sourceId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// missing reloading the relation target note
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'note_contents').forEach(sync => {
|
|
|
|
|
delete this.noteComplementPromises[sync.entityId];
|
|
|
|
|
|
|
|
|
|
loadResults.addNoteContent(sync.entityId, sync.sourceId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'note_revisions').forEach(sync => {
|
|
|
|
|
loadResults.addNoteRevision(sync.entityId, sync.noteId, sync.sourceId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
syncRows.filter(sync => sync.entityName === 'options').forEach(sync => {
|
|
|
|
|
options.set(sync.entity.name, sync.entity.value);
|
|
|
|
|
|
|
|
|
|
loadResults.addOption(sync.entity.name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const appContext = (await import("./app_context.js")).default;
|
|
|
|
|
appContext.trigger('entitiesReloaded', {loadResults});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
logError,
|
|
|
|
|
subscribeToMessages,
|
|
|
|
|
|