Repository navigation
Expand file tree
/
Copy pathmessage-bus.ts
More file actions
88 lines (78 loc) · 2.73 KB
/
Copy pathmessage-bus.ts
File metadata and controls
88 lines (78 loc) · 2.73 KB
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
import PubSub from 'pubsub-js';
/**
* Represents an event in the SDK.
*/
type SdkEvent = string;
export const SDK_EVENTS: { [k: SdkEvent]: SdkEvent } = {
TX_RECEIPT_ACCEPTED: 'TX_RECEIPT_ACCEPTED'
};
/**
* Represents a topic in the SDK message bus.
*/
export type SdkTopic = keyof typeof SDK_EVENTS;
/**
* Represents a message bus that allows publishing and subscribing to topics.
*/
export class MessageBus {
/**
* The singleton instance of the MessageBus class.
*/
private static instance: MessageBus;
/**
* Private constructor for the MessageBus class.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
/**
* Returns the singleton instance of the MessageBus class.
* If the instance doesn't exist, it creates a new one.
* @returns The singleton instance of the MessageBus class.
*/
public static getInstance(): MessageBus {
// If the instance doesn't exist, create it
if (!MessageBus.instance) {
MessageBus.instance = new MessageBus();
}
// Return the instance
return MessageBus.instance;
}
/**
* Publishes a message to the specified topic.
*
* @template T - The type of data being published.
* @param {SdkTopic} topic - The topic to publish the message to.
* @param {T} data - The data to be published.
* @returns {boolean} - Returns true if the message was successfully published, false otherwise.
*/
public publish<T>(topic: SdkTopic, data: T): boolean {
return PubSub.publish(topic.toString(), data);
}
/**
* Subscribes to a specific topic and registers a callback function to be executed when a message is published.
*
* @param topic - The topic to subscribe to.
* @param callback - The callback function to be executed when a message is published.
*/
public subscribe<T>(topic: SdkTopic, callback: (data: T) => void): string {
return PubSub.subscribe(topic.toString(), (_, data) => callback(data));
}
/**
* Subscribes to a specific topic and registers a callback function to be executed when a message is published.
* The callback function is executed only once.
*
* @param topic - The topic to subscribe to.
* @param callback - The callback function to be executed when a message is published.
*/
public subscribeOnce<T>(topic: SdkTopic, callback: (data: T) => void): void {
PubSub.subscribeOnce(topic.toString(), (_, data) => callback(data));
}
/**
* Unsubscribes from a specific topic in the message bus.
*
* @param topic - The topic to unsubscribe from.
* @returns A string or boolean indicating the success of the unsubscribe operation.
*/
public unsubscribe(topic: SdkTopic): string | boolean {
return PubSub.unsubscribe(topic.toString());
}
}