@rimori/client 2.5.5-next.2 → 2.5.5-next.4
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.
|
@@ -24,8 +24,19 @@ export declare class EventBusHandler {
|
|
|
24
24
|
private static instance;
|
|
25
25
|
private debugEnabled;
|
|
26
26
|
private evName;
|
|
27
|
+
private generatedIds;
|
|
28
|
+
private cleanupInterval;
|
|
27
29
|
private constructor();
|
|
28
30
|
static getInstance(name?: string): EventBusHandler;
|
|
31
|
+
/**
|
|
32
|
+
* Starts the interval to cleanup the generated ids.
|
|
33
|
+
*/
|
|
34
|
+
private startIdCleanup;
|
|
35
|
+
/**
|
|
36
|
+
* Generates a unique id.
|
|
37
|
+
* @returns A unique id.
|
|
38
|
+
*/
|
|
39
|
+
private generateUniqueId;
|
|
29
40
|
private createEvent;
|
|
30
41
|
/**
|
|
31
42
|
* Emits an event to the event bus. Can be a new event or a response to a request.
|
|
@@ -13,7 +13,10 @@ export class EventBusHandler {
|
|
|
13
13
|
this.responseResolvers = new Map();
|
|
14
14
|
this.debugEnabled = false;
|
|
15
15
|
this.evName = '';
|
|
16
|
+
this.generatedIds = new Map(); // Map<id, timestamp>
|
|
17
|
+
this.cleanupInterval = null;
|
|
16
18
|
//private constructor
|
|
19
|
+
this.startIdCleanup();
|
|
17
20
|
}
|
|
18
21
|
static getInstance(name) {
|
|
19
22
|
if (!EventBusHandler.instance) {
|
|
@@ -28,8 +31,40 @@ export class EventBusHandler {
|
|
|
28
31
|
}
|
|
29
32
|
return EventBusHandler.instance;
|
|
30
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Starts the interval to cleanup the generated ids.
|
|
36
|
+
*/
|
|
37
|
+
startIdCleanup() {
|
|
38
|
+
this.cleanupInterval = setInterval(() => {
|
|
39
|
+
const now = Date.now();
|
|
40
|
+
const oneMinuteAgo = now - 60000; // 60 seconds in milliseconds
|
|
41
|
+
for (const [id, timestamp] of this.generatedIds.entries()) {
|
|
42
|
+
if (timestamp < oneMinuteAgo) {
|
|
43
|
+
this.generatedIds.delete(id);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}, 10000); // Run every 10 seconds
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Generates a unique id.
|
|
50
|
+
* @returns A unique id.
|
|
51
|
+
*/
|
|
52
|
+
generateUniqueId() {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
const oneMinuteAgo = now - 60000;
|
|
55
|
+
const id = Math.floor(Math.random() * 10000000000);
|
|
56
|
+
// Check if ID was generated within the last minute
|
|
57
|
+
const existingTimestamp = this.generatedIds.get(id);
|
|
58
|
+
if (existingTimestamp && existingTimestamp > oneMinuteAgo) {
|
|
59
|
+
// ID was recently generated, generate a new one recursively
|
|
60
|
+
return this.generateUniqueId();
|
|
61
|
+
}
|
|
62
|
+
// Store the ID with current timestamp
|
|
63
|
+
this.generatedIds.set(id, now);
|
|
64
|
+
return id;
|
|
65
|
+
}
|
|
31
66
|
createEvent(sender, topic, data, eventId) {
|
|
32
|
-
const generatedEventId = eventId ||
|
|
67
|
+
const generatedEventId = eventId || this.generateUniqueId();
|
|
33
68
|
return {
|
|
34
69
|
eventId: generatedEventId,
|
|
35
70
|
timestamp: new Date().toISOString(),
|
|
@@ -101,7 +136,7 @@ export class EventBusHandler {
|
|
|
101
136
|
if (!this.listeners.has(topic)) {
|
|
102
137
|
this.listeners.set(topic, new Set());
|
|
103
138
|
}
|
|
104
|
-
const id =
|
|
139
|
+
const id = this.generateUniqueId();
|
|
105
140
|
// To prevent infinite loops and processing the same eventId multiple times
|
|
106
141
|
const blackListedEventIds = [];
|
|
107
142
|
const eventHandler = (data) => {
|