Notes/src/becca/becca_loader.js

198 lines
5.7 KiB
JavaScript
Raw Normal View History

2020-05-20 00:03:33 +02:00
"use strict";
2021-05-17 22:09:49 +02:00
const sql = require('../services/sql.js');
const eventService = require('../services/events.js');
2021-04-16 23:01:56 +02:00
const becca = require('./becca.js');
2021-05-17 22:09:49 +02:00
const sqlInit = require('../services/sql_init');
const log = require('../services/log');
const Note = require('./entities/note.js');
const Branch = require('./entities/branch.js');
const Attribute = require('./entities/attribute.js');
const Option = require('./entities/option.js');
const cls = require("../services/cls.js");
2020-05-20 00:03:33 +02:00
2021-05-01 21:52:22 +02:00
const beccaLoaded = new Promise((res, rej) => {
sqlInit.dbReady.then(() => {
load();
2021-05-17 22:09:49 +02:00
cls.init(() => require('../services/options_init').initStartupOptions());
2021-05-01 21:52:22 +02:00
res();
});
2020-06-20 21:42:41 +02:00
});
2020-06-20 12:31:38 +02:00
function load() {
const start = Date.now();
2021-04-16 23:00:08 +02:00
becca.reset();
2020-05-20 00:03:33 +02:00
// using raw query and passing arrays to avoid allocating new objects
// this is worth it for becca load since it happens every run and blocks the app until finished
for (const row of sql.getRawRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`, [])) {
new Note().update(row).init();
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
for (const row of sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`, [])) {
new Branch().update(row).init();
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
for (const row of sql.getRawRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`, [])) {
new Attribute().update(row).init();
2021-04-30 23:10:25 +02:00
}
for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
new Option(row);
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
2021-04-16 23:00:08 +02:00
becca.loaded = true;
2021-04-16 23:00:08 +02:00
log.info(`Becca (note cache) load took ${Date.now() - start}ms`);
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_CHANGE_SYNCED], ({entityName, entity}) => {
if (!becca.loaded) {
return;
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (entityName === 'branches') {
branchUpdated(entity);
} else if (entityName === 'attributes') {
attributeUpdated(entity);
} else if (entityName === 'note_reordering') {
noteReorderingUpdated(entity);
}
});
2020-05-20 00:03:33 +02:00
eventService.subscribe([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entityId}) => {
2021-05-01 11:38:20 +02:00
if (!becca.loaded) {
return;
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
if (entityName === 'notes') {
noteDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'branches') {
branchDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'attributes') {
attributeDeleted(entityId);
2021-05-01 11:38:20 +02:00
}
});
function noteDeleted(noteId) {
delete becca.notes[noteId];
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
function branchDeleted(branchId) {
const branch = becca.branches[branchId];
if (!branch) {
return;
}
2021-05-08 23:31:20 +02:00
const childNote = becca.notes[branch.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
childNote.parents = childNote.parents.filter(parent => parent.noteId !== branch.parentNoteId);
childNote.parentBranches = childNote.parentBranches
.filter(parentBranch => parentBranch.branchId !== branch.branchId);
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote.parents.length > 0) {
childNote.invalidateSubTree();
2021-04-30 23:10:25 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
const parentNote = becca.notes[branch.parentNoteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (parentNote) {
parentNote.children = parentNote.children.filter(child => child.noteId !== branch.noteId);
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
delete becca.childParentToBranch[`${branch.noteId}-${branch.parentNoteId}`];
delete becca.branches[branch.branchId];
}
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
function branchUpdated(branch) {
const childNote = becca.notes[branch.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
childNote.flatTextCache = null;
childNote.resortParents();
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
function attributeDeleted(attributeId) {
const attribute = becca.attributes[attributeId];
if (!attribute) {
return;
}
2021-05-01 11:38:20 +02:00
const note = becca.notes[attribute.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
// first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete)
2021-05-17 22:35:36 +02:00
if (attribute.isAffectingSubtree || note.isTemplate()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
} else {
note.invalidateThisCache();
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2021-05-08 23:31:20 +02:00
note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
const targetNote = attribute.targetNote;
2021-05-01 11:38:20 +02:00
if (targetNote) {
2021-05-08 23:31:20 +02:00
targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attribute.attributeId);
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-05-08 23:31:20 +02:00
delete becca.attributes[attribute.attributeId];
2021-05-01 11:38:20 +02:00
const key = `${attribute.type}-${attribute.name.toLowerCase()}`;
if (key in becca.attributeIndex) {
2021-05-08 23:31:20 +02:00
becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
}
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
function attributeUpdated(attribute) {
const note = becca.notes[attribute.noteId];
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
2021-05-17 22:35:36 +02:00
if (attribute.isAffectingSubtree || note.isTemplate()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
2021-04-30 23:10:25 +02:00
} else {
note.invalidateThisCache();
}
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
2021-05-01 11:38:20 +02:00
function noteReorderingUpdated(branchIdList) {
2021-04-30 23:10:25 +02:00
const parentNoteIds = new Set();
2021-05-01 11:38:20 +02:00
for (const branchId in branchIdList) {
2021-04-30 23:10:25 +02:00
const branch = becca.branches[branchId];
2021-04-30 23:10:25 +02:00
if (branch) {
2021-05-01 11:38:20 +02:00
branch.notePosition = branchIdList[branchId];
2021-04-30 23:10:25 +02:00
parentNoteIds.add(branch.parentNoteId);
}
}
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
try {
2021-04-16 23:00:08 +02:00
becca.decryptProtectedNotes();
}
catch (e) {
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
}
2020-05-20 00:03:33 +02:00
});
2020-07-27 23:40:14 +02:00
2021-05-01 11:38:20 +02:00
eventService.subscribe(eventService.LEAVE_PROTECTED_SESSION, load);
2020-07-27 23:40:14 +02:00
module.exports = {
2021-05-01 21:52:22 +02:00
load,
beccaLoaded
2020-07-27 23:40:14 +02:00
};