Notes/src/services/cls.js

87 lines
1.6 KiB
JavaScript
Raw Normal View History

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);
}
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}`);
}
}
}
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');
}
function getLocalNowDateTime() {
return namespace.get('localNowDateTime');
}
function disableEntityEvents() {
namespace.set('disableEntityEvents', true);
}
function isEntityEventsDisabled() {
return !!namespace.get('disableEntityEvents');
}
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);
}
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);
}
module.exports = {
init,
wrap,
2020-06-15 17:56:53 +02:00
get,
set,
2018-03-30 13:20:36 -04:00
namespace,
getSourceId,
getLocalNowDateTime,
disableEntityEvents,
isEntityEventsDisabled,
2020-01-31 22:32:24 +01:00
reset,
getAndClearSyncRows,
2020-04-06 20:59:04 +02:00
addSyncRow,
getEntityFromCache,
setEntityToCache
2020-06-15 17:56:53 +02:00
};