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
/
index.ts
208 lines (174 loc) · 5.66 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Subject, Subscription } from 'rxjs';
import type { Socket } from 'socket.io-client';
import { ErrorCallback } from '../common/types/callbacks.types';
import { InternalRoomEvents, RoomEvents } from '../common/types/event.types';
import { Presence } from '../common/types/presence.types';
import { PresenceRoom } from '../presence';
import { Callback, SocketEvent, JoinRoomPayload, RoomHistory } from './types';
import { Logger } from '../../../common/utils';
export class Room {
private logger: Logger;
private isJoined: boolean = false;
private subscriptions: Map<Callback<unknown>, Subscription> = new Map();
private observers: Map<string, Subject<unknown>> = new Map();
public presence: PresenceRoom;
constructor(
private io: Socket,
private user: Presence,
private roomId: string,
private apiKey: string,
private maxConnections: number | 'unlimited' = 100,
) {
this.logger = new Logger('@superviz/sdk/socket-client/room');
const payload: JoinRoomPayload = {
name: roomId,
user,
maxConnections,
};
this.presence = PresenceRoom.register(io, user, roomId);
this.io.emit(RoomEvents.JOIN_ROOM, payload);
this.subscribeToRoomEvents();
}
public static register(
io: Socket,
presence: Presence,
roomId: string,
apiKey: string,
maxConnections: number | 'unlimited',
): Room {
return new Room(io, presence, roomId, apiKey, maxConnections);
}
/**
* @function on
* @description Listen to an event
* @param event - The event to listen to
* @param callback - The callback to execute when the event is emitted
* @returns {void}
*/
public on<T>(event: string, callback: Callback<T>): void {
this.logger.log('room @ on', event);
let subject = this.observers.get(event);
if (!subject) {
subject = new Subject<T>();
this.observers.set(event, subject);
this.io.on(event, (data: SocketEvent<T>) => {
this.publishEventToClient(event, data);
});
}
this.subscriptions.set(callback, subject.subscribe(callback));
}
/**
* @function off
* @description Stop listening to an event
* @param event - The event to stop listening to
* @param callback - The callback to remove from the event
* @returns {void}
*/
public off<T>(event: string, callback?: Callback<T>): void {
this.logger.log('room @ off', event);
if (!callback) {
this.observers.delete(event);
this.io.off(event);
return;
}
this.subscriptions.get(callback)?.unsubscribe();
}
/**
* @function emit
* @description Emit an event
* @param event - The event to emit
* @param payload - The payload to send with the event
* @returns {void}
*/
public emit<T>(event: string, payload: T): void {
if (!this.isJoined) {
this.logger.log('Cannot emit event. Not joined to room');
return;
}
const body: SocketEvent<T> = {
name: event,
roomId: this.roomId,
presence: this.user,
connectionId: this.io.id,
data: payload,
timestamp: Date.now(),
};
this.io.emit(RoomEvents.UPDATE, this.roomId, body);
this.logger.log('room @ emit', event, payload);
}
/**
* @function get
* @description Get the presences in the room
* @returns {void}
*/
public history(next: (data: RoomHistory) => void, error?: ErrorCallback): void {
const subject = new Subject<RoomHistory>();
subject.subscribe({
next,
error,
});
const callback = (event: RoomHistory) => {
this.logger.log('room @ history', event);
this.io.off(InternalRoomEvents.GET, callback);
subject.next(event);
subject.complete();
subject.unsubscribe();
};
this.io.on(InternalRoomEvents.GET, callback);
this.io.emit(InternalRoomEvents.GET, this.roomId);
}
/**
* @function disconnect
* @description Disconnect from the room
* @returns {void}
*/
public disconnect(): void {
this.logger.log('room @ disconnect', 'Leaving room...', this.roomId);
this.io.emit(RoomEvents.LEAVE_ROOM, this.roomId);
// unsubscribe from all events
this.subscriptions.forEach((subscription) => subscription.unsubscribe());
this.subscriptions.clear();
this.observers.forEach((subject) => subject.unsubscribe());
this.observers.clear();
this.presence.destroy();
}
/**
* @function publishEventToClient
* @description Publish an event to the client
* @param event - The event to publish
* @param data - The data to publish
* @returns {void}
*/
private publishEventToClient(event: string, data: SocketEvent<unknown>): void {
const subject = this.observers.get(event);
if (!subject || data.roomId !== this.roomId) return;
subject.next(data);
}
/*
* @function subscribeToRoomEvents
* @description Subscribe to room events
* @returns {void}
*/
private subscribeToRoomEvents(): void {
this.io.on(RoomEvents.JOINED_ROOM, this.onJoinedRoom);
this.io.on(RoomEvents.ERROR, (event: SocketEvent<string>) => {
this.logger.log('Error:', event.data);
});
this.io.on(`http:${this.roomId}:${this.apiKey}`, this.onHttpEvent);
}
/**
* @function onJoinedRoom
* @description handles the event when a user joins a room.
* @param event The socket event containing presence data.
* @returns {void}
*/
private onJoinedRoom = (event: SocketEvent<{ name: string }>): void => {
if (this.roomId !== event?.data?.name) return;
this.isJoined = true;
this.io.emit(RoomEvents.JOINED_ROOM, this.roomId, event.data);
this.logger.log('room @ joined', event);
};
private onHttpEvent = (event: SocketEvent<unknown>) => {
this.publishEventToClient(event.name, event);
};
}