@mnexium/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ export interface MemoryEvent {
2
+ type: string;
3
+ project_id: string;
4
+ subject_id: string | null;
5
+ data: Record<string, unknown>;
6
+ timestamp: string;
7
+ }
8
+
9
+ type Subscriber = (event: MemoryEvent) => void;
10
+
11
+ function key(projectId: string, subjectId: string | null): string {
12
+ return `${projectId}:${subjectId || "*"}`;
13
+ }
14
+
15
+ export class MemoryEventBus {
16
+ private subscribers = new Map<string, Set<Subscriber>>();
17
+
18
+ subscribe(projectId: string, subjectId: string | null, cb: Subscriber): () => void {
19
+ const k = key(projectId, subjectId);
20
+ if (!this.subscribers.has(k)) this.subscribers.set(k, new Set());
21
+ this.subscribers.get(k)?.add(cb);
22
+ return () => {
23
+ const set = this.subscribers.get(k);
24
+ if (!set) return;
25
+ set.delete(cb);
26
+ if (set.size === 0) this.subscribers.delete(k);
27
+ };
28
+ }
29
+
30
+ emit(projectId: string, subjectId: string | null, type: string, data: Record<string, unknown>) {
31
+ const event: MemoryEvent = {
32
+ type,
33
+ project_id: projectId,
34
+ subject_id: subjectId,
35
+ data,
36
+ timestamp: new Date().toISOString(),
37
+ };
38
+
39
+ const specific = this.subscribers.get(key(projectId, subjectId));
40
+ specific?.forEach((cb) => {
41
+ try {
42
+ cb(event);
43
+ } catch {
44
+ // Ignore callback failures to avoid breaking fan-out.
45
+ }
46
+ });
47
+
48
+ const projectWide = this.subscribers.get(key(projectId, null));
49
+ projectWide?.forEach((cb) => {
50
+ try {
51
+ cb(event);
52
+ } catch {
53
+ // Ignore callback failures to avoid breaking fan-out.
54
+ }
55
+ });
56
+ }
57
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "lib": ["ES2022"],
7
+ "strict": false,
8
+ "skipLibCheck": true,
9
+ "esModuleInterop": true,
10
+ "resolveJsonModule": true,
11
+ "noEmit": true
12
+ },
13
+ "include": ["src/**/*.ts"]
14
+ }