Notes/src/services/cls.ts

120 lines
2.6 KiB
TypeScript
Raw Normal View History

import clsHooked from "cls-hooked";
import { EntityChange } from './entity_changes_interface.js';
const namespace = clsHooked.createNamespace("trilium");
2024-02-16 21:16:35 +02:00
type Callback = (...args: any[]) => any;
function init(callback: Callback) {
2020-06-20 12:31:38 +02:00
return namespace.runAndReturn(callback);
}
2024-02-16 21:16:35 +02:00
function wrap(callback: Callback) {
2020-09-01 00:05:19 +02:00
return () => {
try {
init(callback);
}
2024-02-16 21:16:35 +02:00
catch (e: any) {
2020-09-01 00:05:19 +02:00
console.log(`Error occurred: ${e.message}: ${e.stack}`);
}
}
}
2024-02-16 21:16:35 +02:00
function get(key: string) {
2020-06-15 17:56:53 +02:00
return namespace.get(key);
}
2024-02-16 21:16:35 +02:00
function set(key: string, value: any) {
2020-06-15 17:56:53 +02:00
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
}
function getComponentId() {
return namespace.get('componentId');
2018-03-30 13:20:36 -04:00
}
function getLocalNowDateTime() {
return namespace.get('localNowDateTime');
}
function disableEntityEvents() {
namespace.set('disableEntityEvents', true);
}
function enableEntityEvents() {
namespace.set('disableEntityEvents', false);
}
function isEntityEventsDisabled() {
return !!namespace.get('disableEntityEvents');
}
2024-02-16 21:16:35 +02:00
function setMigrationRunning(running: boolean) {
namespace.set('migrationRunning', !!running);
}
function isMigrationRunning() {
return !!namespace.get('migrationRunning');
}
2024-02-16 21:16:35 +02:00
function disableSlowQueryLogging(disable: boolean) {
2023-10-20 09:36:57 +02:00
namespace.set('disableSlowQueryLogging', disable);
}
function isSlowQueryLoggingDisabled() {
return !!namespace.get('disableSlowQueryLogging');
}
function getAndClearEntityChangeIds() {
const entityChangeIds = namespace.get('entityChangeIds') || [];
namespace.set('entityChangeIds', []);
return entityChangeIds;
2020-01-31 22:32:24 +01:00
}
function putEntityChange(entityChange: EntityChange) {
if (namespace.get('ignoreEntityChangeIds')) {
return;
}
const entityChangeIds = namespace.get('entityChangeIds') || [];
2020-01-31 22:32:24 +01:00
2023-05-05 23:41:11 +02:00
// store only ID since the record can be modified (e.g., in erase)
entityChangeIds.push(entityChange.id);
2020-01-31 22:32:24 +01:00
namespace.set('entityChangeIds', entityChangeIds);
2020-01-31 22:32:24 +01:00
}
function reset() {
clsHooked.reset();
}
function ignoreEntityChangeIds() {
namespace.set('ignoreEntityChangeIds', true);
2020-04-06 20:59:04 +02:00
}
export default {
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,
getComponentId,
getLocalNowDateTime,
disableEntityEvents,
enableEntityEvents,
isEntityEventsDisabled,
2020-01-31 22:32:24 +01:00
reset,
getAndClearEntityChangeIds,
2023-07-29 23:25:02 +02:00
putEntityChange,
2023-02-01 21:07:23 +01:00
ignoreEntityChangeIds,
2023-10-20 09:36:57 +02:00
disableSlowQueryLogging,
isSlowQueryLoggingDisabled,
setMigrationRunning,
isMigrationRunning
2020-06-15 17:56:53 +02:00
};