119 lines
2.6 KiB
TypeScript
Raw Normal View History

import clsHooked from "cls-hooked";
import type { 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);
2025-01-09 18:07:02 +02:00
} catch (e: any) {
2020-09-01 00:05:19 +02:00
console.log(`Error occurred: ${e.message}: ${e.stack}`);
}
2025-01-09 18:07:02 +02:00
};
}
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() {
2025-01-09 18:07:02 +02:00
return namespace.get("hoistedNoteId") || "root";
2020-11-23 22:52:48 +01:00
}
function getComponentId() {
2025-01-09 18:07:02 +02:00
return namespace.get("componentId");
2018-03-30 13:20:36 -04:00
}
function getLocalNowDateTime() {
2025-01-09 18:07:02 +02:00
return namespace.get("localNowDateTime");
}
function disableEntityEvents() {
2025-01-09 18:07:02 +02:00
namespace.set("disableEntityEvents", true);
}
function enableEntityEvents() {
2025-01-09 18:07:02 +02:00
namespace.set("disableEntityEvents", false);
}
function isEntityEventsDisabled() {
2025-01-09 18:07:02 +02:00
return !!namespace.get("disableEntityEvents");
}
2024-02-16 21:16:35 +02:00
function setMigrationRunning(running: boolean) {
2025-01-09 18:07:02 +02:00
namespace.set("migrationRunning", !!running);
}
function isMigrationRunning() {
2025-01-09 18:07:02 +02:00
return !!namespace.get("migrationRunning");
}
2024-02-16 21:16:35 +02:00
function disableSlowQueryLogging(disable: boolean) {
2025-01-09 18:07:02 +02:00
namespace.set("disableSlowQueryLogging", disable);
2023-10-20 09:36:57 +02:00
}
function isSlowQueryLoggingDisabled() {
2025-01-09 18:07:02 +02:00
return !!namespace.get("disableSlowQueryLogging");
2023-10-20 09:36:57 +02:00
}
function getAndClearEntityChangeIds() {
2025-01-09 18:07:02 +02:00
const entityChangeIds = namespace.get("entityChangeIds") || [];
2025-01-09 18:07:02 +02:00
namespace.set("entityChangeIds", []);
return entityChangeIds;
2020-01-31 22:32:24 +01:00
}
function putEntityChange(entityChange: EntityChange) {
2025-01-09 18:07:02 +02:00
if (namespace.get("ignoreEntityChangeIds")) {
return;
}
2025-01-09 18:07:02 +02:00
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
2025-01-09 18:07:02 +02:00
namespace.set("entityChangeIds", entityChangeIds);
2020-01-31 22:32:24 +01:00
}
function reset() {
clsHooked.reset();
}
function ignoreEntityChangeIds() {
2025-01-09 18:07:02 +02:00
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
};