mirror of
https://github.com/zadam/trilium.git
synced 2025-11-14 09:15:50 +01:00
server-ts: cls.js -> cls.ts
This commit is contained in:
119
src/services/cls.ts
Normal file
119
src/services/cls.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import clsHooked = require('cls-hooked');
|
||||
import type entity_changes = require('./entity_changes');
|
||||
const namespace = clsHooked.createNamespace("trilium");
|
||||
|
||||
type Callback = (...args: any[]) => any;
|
||||
|
||||
function init(callback: Callback) {
|
||||
return namespace.runAndReturn(callback);
|
||||
}
|
||||
|
||||
function wrap(callback: Callback) {
|
||||
return () => {
|
||||
try {
|
||||
init(callback);
|
||||
}
|
||||
catch (e: any) {
|
||||
console.log(`Error occurred: ${e.message}: ${e.stack}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function get(key: string) {
|
||||
return namespace.get(key);
|
||||
}
|
||||
|
||||
function set(key: string, value: any) {
|
||||
namespace.set(key, value);
|
||||
}
|
||||
|
||||
function getHoistedNoteId() {
|
||||
return namespace.get('hoistedNoteId') || 'root';
|
||||
}
|
||||
|
||||
function getComponentId() {
|
||||
return namespace.get('componentId');
|
||||
}
|
||||
|
||||
function getLocalNowDateTime() {
|
||||
return namespace.get('localNowDateTime');
|
||||
}
|
||||
|
||||
function disableEntityEvents() {
|
||||
namespace.set('disableEntityEvents', true);
|
||||
}
|
||||
|
||||
function enableEntityEvents() {
|
||||
namespace.set('disableEntityEvents', false);
|
||||
}
|
||||
|
||||
function isEntityEventsDisabled() {
|
||||
return !!namespace.get('disableEntityEvents');
|
||||
}
|
||||
|
||||
function setMigrationRunning(running: boolean) {
|
||||
namespace.set('migrationRunning', !!running);
|
||||
}
|
||||
|
||||
function isMigrationRunning() {
|
||||
return !!namespace.get('migrationRunning');
|
||||
}
|
||||
|
||||
function disableSlowQueryLogging(disable: boolean) {
|
||||
namespace.set('disableSlowQueryLogging', disable);
|
||||
}
|
||||
|
||||
function isSlowQueryLoggingDisabled() {
|
||||
return !!namespace.get('disableSlowQueryLogging');
|
||||
}
|
||||
|
||||
function getAndClearEntityChangeIds() {
|
||||
const entityChangeIds = namespace.get('entityChangeIds') || [];
|
||||
|
||||
namespace.set('entityChangeIds', []);
|
||||
|
||||
return entityChangeIds;
|
||||
}
|
||||
|
||||
function putEntityChange(entityChange: entity_changes.EntityChange) {
|
||||
if (namespace.get('ignoreEntityChangeIds')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entityChangeIds = namespace.get('entityChangeIds') || [];
|
||||
|
||||
// store only ID since the record can be modified (e.g., in erase)
|
||||
entityChangeIds.push(entityChange.id);
|
||||
|
||||
namespace.set('entityChangeIds', entityChangeIds);
|
||||
}
|
||||
|
||||
function reset() {
|
||||
clsHooked.reset();
|
||||
}
|
||||
|
||||
function ignoreEntityChangeIds() {
|
||||
namespace.set('ignoreEntityChangeIds', true);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init,
|
||||
wrap,
|
||||
get,
|
||||
set,
|
||||
namespace,
|
||||
getHoistedNoteId,
|
||||
getComponentId,
|
||||
getLocalNowDateTime,
|
||||
disableEntityEvents,
|
||||
enableEntityEvents,
|
||||
isEntityEventsDisabled,
|
||||
reset,
|
||||
getAndClearEntityChangeIds,
|
||||
putEntityChange,
|
||||
ignoreEntityChangeIds,
|
||||
disableSlowQueryLogging,
|
||||
isSlowQueryLoggingDisabled,
|
||||
setMigrationRunning,
|
||||
isMigrationRunning
|
||||
};
|
||||
Reference in New Issue
Block a user