2018-03-28 23:41:22 -04:00
|
|
|
const clsHooked = require('cls-hooked');
|
|
|
|
const namespace = clsHooked.createNamespace("trilium");
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function init(callback) {
|
|
|
|
return namespace.runAndReturn(callback);
|
2018-03-28 23:41:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function wrap(callback) {
|
2020-09-01 00:05:19 +02:00
|
|
|
return () => {
|
|
|
|
try {
|
|
|
|
init(callback);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(`Error occurred: ${e.message}: ${e.stack}`);
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 23:41:22 -04:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:56:53 +02:00
|
|
|
function get(key) {
|
|
|
|
return namespace.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set(key, value) {
|
|
|
|
namespace.set(key, value);
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:52:48 +01:00
|
|
|
function getHoistedNoteId() {
|
2020-12-14 23:59:05 +01:00
|
|
|
return namespace.get('hoistedNoteId') || 'root';
|
2020-11-23 22:52:48 +01:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
function getSourceId() {
|
|
|
|
return namespace.get('sourceId');
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:26:57 +02:00
|
|
|
function getLocalNowDateTime() {
|
|
|
|
return namespace.get('localNowDateTime');
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
function disableEntityEvents() {
|
|
|
|
namespace.set('disableEntityEvents', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEntityEventsDisabled() {
|
|
|
|
return !!namespace.get('disableEntityEvents');
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:48:14 +01:00
|
|
|
function clearEntityChanges() {
|
|
|
|
namespace.set('entityChanges', []);
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
function getAndClearEntityChanges() {
|
|
|
|
const entityChanges = namespace.get('entityChanges') || [];
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2021-03-12 23:48:14 +01:00
|
|
|
clearEntityChanges();
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
return entityChanges;
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
function addEntityChange(entityChange) {
|
2021-02-16 22:51:00 +01:00
|
|
|
if (namespace.get('ignoreEntityChanges')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
const entityChanges = namespace.get('entityChanges') || [];
|
2020-01-31 22:32:24 +01:00
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
entityChanges.push(entityChange);
|
2020-01-31 22:32:24 +01:00
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
namespace.set('entityChanges', entityChanges);
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
2018-07-22 19:56:20 +02:00
|
|
|
function reset() {
|
|
|
|
clsHooked.reset();
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:51:00 +01:00
|
|
|
function ignoreEntityChanges() {
|
|
|
|
namespace.set('ignoreEntityChanges', true);
|
2020-04-06 20:59:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 23:41:22 -04:00
|
|
|
module.exports = {
|
|
|
|
init,
|
|
|
|
wrap,
|
2020-06-15 17:56:53 +02:00
|
|
|
get,
|
|
|
|
set,
|
2018-03-30 13:20:36 -04:00
|
|
|
namespace,
|
2020-11-23 22:52:48 +01:00
|
|
|
getHoistedNoteId,
|
2018-07-22 19:56:20 +02:00
|
|
|
getSourceId,
|
2020-04-26 14:26:57 +02:00
|
|
|
getLocalNowDateTime,
|
2018-11-26 22:27:57 +01:00
|
|
|
disableEntityEvents,
|
|
|
|
isEntityEventsDisabled,
|
2020-01-31 22:32:24 +01:00
|
|
|
reset,
|
2021-03-12 23:48:14 +01:00
|
|
|
clearEntityChanges,
|
2020-12-14 14:17:51 +01:00
|
|
|
getAndClearEntityChanges,
|
|
|
|
addEntityChange,
|
2021-02-16 22:51:00 +01:00
|
|
|
ignoreEntityChanges
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|