2024-07-18 21:37:45 +03:00
|
|
|
import clsHooked from "cls-hooked";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { EntityChange } from "./entity_changes_interface.js";
|
2018-03-28 23:41:22 -04:00
|
|
|
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);
|
2018-03-28 23:41:22 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
2018-03-28 23:41:22 -04: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
|
|
|
}
|
|
|
|
|
2022-01-09 20:16:39 +01:00
|
|
|
function getComponentId() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return namespace.get("componentId");
|
2018-03-30 13:20:36 -04:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:26:57 +02:00
|
|
|
function getLocalNowDateTime() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return namespace.get("localNowDateTime");
|
2020-04-26 14:26:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
function disableEntityEvents() {
|
2025-01-09 18:07:02 +02:00
|
|
|
namespace.set("disableEntityEvents", true);
|
2018-11-26 22:27:57 +01:00
|
|
|
}
|
|
|
|
|
2022-10-22 21:34:38 +02:00
|
|
|
function enableEntityEvents() {
|
2025-01-09 18:07:02 +02:00
|
|
|
namespace.set("disableEntityEvents", false);
|
2022-10-22 21:34:38 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
function isEntityEventsDisabled() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return !!namespace.get("disableEntityEvents");
|
2018-11-26 22:27:57 +01:00
|
|
|
}
|
|
|
|
|
2024-02-16 21:16:35 +02:00
|
|
|
function setMigrationRunning(running: boolean) {
|
2025-01-09 18:07:02 +02:00
|
|
|
namespace.set("migrationRunning", !!running);
|
2024-01-21 20:50:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isMigrationRunning() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return !!namespace.get("migrationRunning");
|
2024-01-21 20:50:38 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
function getAndClearEntityChangeIds() {
|
2025-01-09 18:07:02 +02:00
|
|
|
const entityChangeIds = namespace.get("entityChangeIds") || [];
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
namespace.set("entityChangeIds", []);
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
return entityChangeIds;
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
2024-02-17 11:42:19 +02:00
|
|
|
function putEntityChange(entityChange: EntityChange) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (namespace.get("ignoreEntityChangeIds")) {
|
2021-02-16 22:51:00 +01:00
|
|
|
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)
|
2021-09-16 15:02:20 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-22 19:56:20 +02:00
|
|
|
function reset() {
|
|
|
|
clsHooked.reset();
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
function ignoreEntityChangeIds() {
|
2025-01-09 18:07:02 +02:00
|
|
|
namespace.set("ignoreEntityChangeIds", true);
|
2020-04-06 20:59:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-03-28 23:41:22 -04:00
|
|
|
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,
|
2022-01-09 20:16:39 +01:00
|
|
|
getComponentId,
|
2020-04-26 14:26:57 +02:00
|
|
|
getLocalNowDateTime,
|
2018-11-26 22:27:57 +01:00
|
|
|
disableEntityEvents,
|
2022-10-22 21:34:38 +02:00
|
|
|
enableEntityEvents,
|
2018-11-26 22:27:57 +01:00
|
|
|
isEntityEventsDisabled,
|
2020-01-31 22:32:24 +01:00
|
|
|
reset,
|
2021-09-16 15:02:20 +02:00
|
|
|
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,
|
2024-01-21 20:50:38 +01:00
|
|
|
isSlowQueryLoggingDisabled,
|
|
|
|
setMigrationRunning,
|
|
|
|
isMigrationRunning
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|