This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
113 lines (92 loc) · 3.27 KB
/
index.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
import { Logger, Observer } from '../../common/utils';
import { AblyRealtimeService } from '../realtime';
import { RealtimeMessage } from '../realtime/ably/types';
export class PubSub {
private readonly realtime: AblyRealtimeService;
private readonly logger: Logger;
private observers: Map<string, Observer> = new Map();
constructor(realtime: AblyRealtimeService) {
this.logger = new Logger('@superviz/sdk/pubsub');
this.realtime = realtime;
this.realtime.syncPropertiesObserver.subscribe(this.onSyncPropertiesChange);
this.logger.log('PubSub created');
}
/**
* @function subscribe
* @description subscribe to event
* @param event - event name
* @param callback - callback function
* @returns {void}
*/
public subscribe = (event: string, callback: (data: unknown) => void): void => {
this.logger.log('pubsub service @ subscribe', { event, callback });
if (!this.observers.has(event)) {
this.observers.set(event, new Observer());
}
this.observers.get(event).subscribe(callback);
};
/**
* @function unsubscribe
* @description - unsubscribe from event
* @param event - event name
* @param callback - callback function
* @returns {void}
*/
public unsubscribe = (event: string, callback: (data: unknown) => void): void => {
this.logger.log('pubsub service @ unsubscribe', { event, callback });
if (!this.observers.has(event)) return;
this.observers.get(event).reset();
this.observers.delete(event);
};
/**
* @function publish
* @description - publish event to realtime
* @param event - event name
* @param data - data to publish
* @returns {void}
*/
public publish = (event: string, data: unknown): void => {
this.logger.log('pubsub service @ publish', { event, data });
this.realtime.setSyncProperty(event, data);
};
/**
* @function fetchSyncClientProperty
* @description get realtime client data history
* @returns {RealtimeMessage | Record<string, RealtimeMessage>}
*/
public fetchHistory(
eventName?: string,
): Promise<RealtimeMessage | Record<string, RealtimeMessage>> {
return this.realtime.fetchSyncClientProperty(eventName);
}
/**
* @function publishEventToClient
* @description - publish event to client
* @param event - event name
* @param data - data to publish
* @returns {void}
*/
public publishEventToClient = (event: string, data?: unknown): void => {
this.logger.log('pubsub service @ publishEventToClient', { event, data });
if (!this.observers.has(event)) return;
this.observers.get(event).publish(data);
};
public destroy = (): void => {
this.logger.log('pubsub service @ destroy');
this.realtime.syncPropertiesObserver.unsubscribe(this.onSyncPropertiesChange);
this.observers.forEach((observer) => observer.destroy());
this.observers.clear();
};
/**
* @function onSyncPropertiesChange
* @description - sync properties change handler
* @param properties - properties
* @returns {void}
*/
private onSyncPropertiesChange = (properties: Record<string, unknown>): void => {
this.logger.log('pubsub service @ onSyncPropertiesChange', { properties });
Object.entries(properties).forEach(([key, value]) => {
this.publishEventToClient(key, value);
});
};
}