Notes/src/services/cls.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

const clsHooked = require('cls-hooked');
const namespace = clsHooked.createNamespace("trilium");
async function init(callback) {
return await namespace.runAndReturn(callback);
}
function wrap(callback) {
return async () => await init(callback);
}
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');
}
2020-01-31 22:32:24 +01:00
function getSyncRows() {
return namespace.get('syncRows') || [];
}
function addSyncRow(syncRow) {
const syncRows = getSyncRows();
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,
2018-03-30 13:20:36 -04:00
namespace,
getSourceId,
getLocalNowDateTime,
disableEntityEvents,
isEntityEventsDisabled,
2020-01-31 22:32:24 +01:00
reset,
getSyncRows,
2020-04-06 20:59:04 +02:00
addSyncRow,
getEntityFromCache,
setEntityToCache
};