restore all "named" notes quickly after their deletion, #3517

This commit is contained in:
zadam 2023-01-13 11:53:25 +01:00
parent 60602a2264
commit 082caf98e8
3 changed files with 38 additions and 1 deletions

View file

@ -4,6 +4,8 @@ const treeService = require('./tree');
const noteService = require('./notes');
const becca = require('../becca/becca');
const Attribute = require('../becca/entities/attribute');
const hiddenSubtreeService = require("./hidden_subtree");
const oneTimeTimer = require("./one_time_timer");
function runAttachedRelations(note, relationName, originEntity) {
if (!note) {
@ -206,6 +208,16 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>
if (entityName === 'branches') {
runAttachedRelations(entity.getNote(), 'runOnBranchDeletion', entity);
}
if (entityName === 'notes' && entity.noteId.startsWith("_")) {
// "named" note has been deleted, we will probably need to rebuild the hidden subtree
// scheduling so that bulk deletes won't trigger so many checks
oneTimeTimer.scheduleExecution('hidden-subtree-check', 1000, () => {
console.log("Checking hidden subtree");
hiddenSubtreeService.checkHiddenSubtree();
});
}
});
module.exports = {

View file

@ -0,0 +1,25 @@
const scheduledExecutions = {};
/**
* Subsequent calls will not move the timer to future. The first caller determines the time of execution.
*
* The good thing about synchronous better-sqlite3 is that this cannot interrupt transaction. The execution will be called
* only outside of a transaction.
*/
function scheduleExecution(name, milliseconds, cb) {
if (name in scheduledExecutions) {
return;
}
scheduledExecutions[name] = true;
setTimeout(() => {
delete scheduledExecutions[name];
cb();
}, milliseconds);
}
module.exports = {
scheduleExecution
};

View file

@ -218,7 +218,7 @@ function resetLauncher(noteId) {
log.info(`Note ${noteId} is not a resettable launcher note.`);
}
hiddenSubtreeService.checkHiddenSubtree();
// the re-building deleted launchers will be done in handlers
}
/**