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
414 lines (344 loc) · 11.8 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '@superviz/socket-client';
import { TranscriptState } from '../../common/types/events.types';
import { ParticipantType, VideoParticipant } from '../../common/types/participant.types';
import { RealtimeStateTypes } from '../../common/types/realtime.types';
import { StoreType } from '../../common/types/stores.types';
import { Logger, Observer } from '../../common/utils';
import { useStore } from '../../common/utils/use-store';
import { DrawingData } from '../video-conference-manager/types';
import { RoomPropertiesEvents, VideoRoomProperties } from './type';
export class RoomStateService {
private room: Room;
private logger: Logger;
private myParticipant: Partial<VideoParticipant> = {};
private localRoomProperties: VideoRoomProperties = null;
private drawingData: DrawingData = null;
private enableSync: boolean;
private left: boolean;
private isSyncFrozen: boolean;
private state: RealtimeStateTypes = RealtimeStateTypes.DISCONNECTED;
private readonly MESSAGE_SIZE_LIMIT = 60000;
private useStore: typeof useStore = useStore.bind(this);
public kickParticipantObserver: Observer;
private started: boolean;
private drawingRoom: Room;
constructor(room: Room, drawingRoom: Room, logger: Logger) {
this.room = room;
this.drawingRoom = drawingRoom;
this.logger = logger;
this.kickParticipantObserver = new Observer({ logger: this.logger });
const { localParticipant, participants } = this.useStore(StoreType.GLOBAL);
localParticipant.subscribe((participant) => {
this.enableSync = participant.type !== ParticipantType.AUDIENCE;
this.myParticipant = {
...this.myParticipant,
...participant,
};
});
participants.subscribe();
this.join();
}
/**
* @function join
* @description subscribes to room events
* @returns {void}
*/
private join = (): void => {
this.room.presence.on(PresenceEvents.LEAVE, this.onParticipantLeave);
this.room.presence.on(PresenceEvents.JOINED_ROOM, this.onPresenceEnter);
if (!this.enableSync) return;
this.room.on(RoomPropertiesEvents.UPDATE, this.updateLocalRoomState);
this.drawingRoom.presence.on(PresenceEvents.UPDATE, this.updateDrawing);
};
/**
* @function updateMyProperties
* @param {Partial<VideoParticipant>} newProperties
* @description updates local participant properties
* @returns {void}
*/
public updateMyProperties = (newProperties?: Partial<VideoParticipant>): void => {
const properties = newProperties ?? ({} as VideoParticipant);
if (this.isMessageTooBig(properties) || this.left || !this.enableSync || this.isSyncFrozen) {
return;
}
if (properties.avatar === undefined) {
delete properties.avatar;
}
this.myParticipant = {
...this.myParticipant,
...properties,
};
this.room.presence.update(this.myParticipant);
this.logger.log('REALTIME', 'updating my properties', this.myParticipant);
};
/**
* @function isMessageTooBig
* @description calculates the size of a sync message and checks if it's bigger than limit
* @param {unknown} msg
* @param {number} limit
* @returns {boolean}
*/
private isMessageTooBig = (msg: unknown, limit: number = this.MESSAGE_SIZE_LIMIT): boolean => {
const messageString = JSON.stringify(msg);
const size = new TextEncoder().encode(messageString).length;
if (size > limit) {
this.logger.log('Message too long, the message limit size is 60kb.');
return true;
}
return false;
};
/**
* @function updateRoomProperties
* @param {VideoRoomProperties} properties
* @description updates room properties
* @returns {void}
*/
public updateRoomProperties = (properties: VideoRoomProperties): void => {
if (this.isMessageTooBig(properties) || this.isSyncFrozen || this.left) return;
const newProperties = {
...this.localRoomProperties,
...properties,
};
this.localRoomProperties = newProperties;
this.room.emit(RoomPropertiesEvents.UPDATE, newProperties);
};
/**
* @function updateDrawingProperties
* @param {DrawingData} data
* @description updates drawing properties
* @returns {void}
*/
private updateDrawingProperties = (data: DrawingData): void => {
if (this.isMessageTooBig(data) || this.isSyncFrozen || this.left) return;
this.drawingData = {
...this.drawingData,
...data,
};
this.drawingRoom.presence.update(this.drawingData);
};
/**
* @function setHost
* @param {string} participantId
* @description set a new host to the room
* @returns {void}
*/
public setHost = (participantId: string): void => {
this.updateRoomProperties({ hostClientId: participantId });
};
/**
* @function setKickParticipant
* @param {string} kickParticipantId
* @description set a participant to be kicked from the room
* @returns {void}
*/
public setKickParticipant = (kickParticipantId: string): Promise<void> => {
if (!kickParticipantId) return;
const { participants } = useStore(StoreType.GLOBAL);
const participant = participants.value[kickParticipantId];
this.updateRoomProperties({
// @ts-ignore
kickParticipant: participant,
});
};
/**
* @function setGridMode
* @param {boolean} isGridModeEnabled
* @description synchronizes the grid mode of the cameras in the room
* @returns {void}
*/
public setGridMode(isGridModeEnabled: boolean): void {
this.updateRoomProperties({ isGridModeEnabled });
}
/**
* @function setDrawing
* @param drawing {DrawingData} - drawing payload*
* @description synchronizes the drawing in the room
* @returns {void}
*/
public setDrawing(drawing: DrawingData): void {
this.updateDrawingProperties(drawing);
}
/**
* @function setTranscript
* @param state {TranscriptState}
* @description synchronizes the transcript state in the room
* @returns {void}
*/
public setTranscript(state: TranscriptState): void {
this.updateRoomProperties({ transcript: state });
}
/**
* @function initializeRoomProperties
* @description
Initializes the room properties,
including setting the host client ID and updating the participant list.
* @returns {Promise<void>}
*/
private initializeRoomProperties = (): void => {
const roomProperties: VideoRoomProperties = {
isGridModeEnabled: false,
hostClientId: null,
followParticipantId: null,
gather: false,
transcript: TranscriptState.TRANSCRIPT_STOP,
kickParticipant: null,
};
this.localRoomProperties = roomProperties;
this.updateRoomProperties(roomProperties);
};
/**
* @function onParticipantLeave
* @returns {void}
* @param presence
*/
private onParticipantLeave = (presence: PresenceEvent): void => {
if (presence.id === this.myParticipant.id) {
this.left = true;
}
const followedLeft = presence.id === this.localRoomProperties?.followParticipantId;
if (followedLeft) {
this.setFollowParticipant();
}
};
/**
* @function fetchRoomProperties
* @returns {VideoRoomProperties | null}
*/
private async fetchRoomProperties(): Promise<unknown | null> {
const presences: number = await new Promise<number>((resolve) => {
this.room.presence.get((presences) => {
if (!presences) resolve(0);
resolve(presences.length);
});
});
if (presences <= 1) return null;
const lastMessage: SocketEvent<unknown> = await new Promise((resolve, reject) => {
this.room.history((data) => {
if (!data) reject(data);
if (!data.events.length) resolve(null);
const lastMessage = data.events.pop();
resolve(lastMessage);
});
});
const oneHour = 1000 * 60 * 60;
const messageIsTooOld = lastMessage?.timestamp < Date.now() - oneHour;
if (!lastMessage?.data || messageIsTooOld) return null;
return lastMessage.data;
}
/**
* @function start
* @returns {Promise<void>}
*/
public start = async (): Promise<void> => {
if (this.started) return;
this.started = true;
if (!this.room['isJoined']) {
this.logger.log('room state service - not joined room yet');
setTimeout(() => {
this.logger.log('room state service - retrying');
this.start();
}, 2000);
}
this.localRoomProperties = await this.fetchRoomProperties();
if (!this.localRoomProperties) {
this.initializeRoomProperties();
} else {
this.updateLocalRoomState({ data: this.localRoomProperties });
}
this.publishStateUpdate(RealtimeStateTypes.CONNECTED);
this.logger.log('REALTIME', 'Joined realtime room');
};
/**
* @function publishStateUpdate
* @description saves the room locally and publishes it to the sdk
* @param {RealtimeStateTypes} state
* @returns
*/
private publishStateUpdate(state: RealtimeStateTypes): void {
if (this.state === state) return;
this.state = state;
this.logger.log(
'REALTIME',
`Realtime state did change. New state: ${RealtimeStateTypes[this.state]}`,
);
const { meetingState } = this.useStore(StoreType.VIDEO);
meetingState.publish(this.state);
}
/**
* @function onAblyPresenceEnter
* @description callback that receives the event that a participant has entered the room
* @returns {void}
*/
private onPresenceEnter = (): void => {
this.updateMyProperties();
};
/**
* @function setFollowParticipant
* @param {string} participantId
* @description add/change and sync a property in the room
* @returns {void}
*/
public setFollowParticipant(participantId?: string): void {
this.updateRoomProperties({ followParticipantId: participantId });
}
/**
* @function setGather
* @param {boolean} active
* @description sync to all participants to go to the host position
* @returns {void}
*/
public setGather(active: boolean): void {
this.updateRoomProperties({ gather: active });
}
/**
* @function updateLocalRoomState
* @description update room data
* @param {VideoRoomProperties} data
* @returns {void}
*/
private updateLocalRoomState = async ({ data }: { data: VideoRoomProperties }): Promise<void> => {
this.logger.log('REALTIME', 'Room update received', data);
this.localRoomProperties = Object.assign({}, this.localRoomProperties, data);
const { followParticipantId, gather, hostId, isGridModeEnabled, transcript } = this.useStore(
StoreType.VIDEO,
);
followParticipantId.publish(data.followParticipantId);
gather.publish(data.gather);
hostId.publish(data.hostClientId);
isGridModeEnabled.publish(data.isGridModeEnabled);
transcript.publish(data.transcript);
if (data.kickParticipant && data.kickParticipant.id === this.myParticipant.id) {
this.updateRoomProperties({ kickParticipant: null });
this.kickParticipantObserver.publish(this.myParticipant.id);
}
};
private updateDrawing = (event: PresenceEvent<DrawingData>): void => {
if (event.id === this.myParticipant.id) return;
const { drawing } = this.useStore(StoreType.VIDEO);
drawing.publish(event.data);
};
/**
* @function freezeSync
* @param {boolean} isFrozen
* @description Detaches and unsubscribes from channels to freeze synchronization with the room.
* @returns {void}
*/
public freezeSync = (isFrozen: boolean): void => {
this.isSyncFrozen = isFrozen;
if (isFrozen) {
this.destroy();
return;
}
this.join();
};
/**
* @function destroy
* @description stopsthe service
*/
public destroy() {
this.room.presence.off(PresenceEvents.LEAVE);
this.room.presence.off(PresenceEvents.JOINED_ROOM);
this.drawingRoom.presence.off(PresenceEvents.UPDATE);
this.room.off(RoomPropertiesEvents.UPDATE, this.updateLocalRoomState);
}
}