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-06-20 12:31:38 +02:00
|
|
|
return () => init(callback);
|
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);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:44:47 +02:00
|
|
|
function getAndClearSyncRows() {
|
|
|
|
const syncRows = namespace.get('syncRows') || [];
|
|
|
|
|
|
|
|
namespace.set('syncRows', []);
|
|
|
|
|
|
|
|
return syncRows;
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function addSyncRow(syncRow) {
|
2020-06-21 23:41:51 +02:00
|
|
|
const syncRows = namespace.get('syncRows') || [];
|
2020-01-31 22:32:24 +01:00
|
|
|
|
|
|
|
syncRows.push(syncRow);
|
|
|
|
|
|
|
|
namespace.set('syncRows', syncRows);
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:56:20 +02:00
|
|
|
function reset() {
|
|
|
|
clsHooked.reset();
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:59:04 +02:00
|
|
|
function getEntityFromCache(entityName, entityId) {
|
|
|
|
return namespace.get(entityName + '-' + entityId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEntityToCache(entityName, entityId, entity) {
|
|
|
|
return namespace.set(entityName + '-' + entityId, entity);
|
|
|
|
}
|
|
|
|
|
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,
|
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,
|
2020-06-21 13:44:47 +02:00
|
|
|
getAndClearSyncRows,
|
2020-04-06 20:59:04 +02:00
|
|
|
addSyncRow,
|
|
|
|
getEntityFromCache,
|
|
|
|
setEntityToCache
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|