-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathcls.ts
118 lines (93 loc) · 2.57 KB
/
cls.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import clsHooked from "cls-hooked";
import type { EntityChange } from "./entity_changes_interface.js";
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: 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);
}
export default {
init,
wrap,
get,
set,
namespace,
getHoistedNoteId,
getComponentId,
getLocalNowDateTime,
disableEntityEvents,
enableEntityEvents,
isEntityEventsDisabled,
reset,
getAndClearEntityChangeIds,
putEntityChange,
ignoreEntityChangeIds,
disableSlowQueryLogging,
isSlowQueryLoggingDisabled,
setMigrationRunning,
isMigrationRunning
};