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
217 lines (172 loc) · 5.63 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
import * as Socket from '../../lib/socket';
import {
NAME_IS_WHITE_TEXT,
MEETING_COLORS,
MEETING_COLORS_KEYS,
} from '../../common/types/meeting-colors.types';
import { Participant, ParticipantType, Slot } from '../../common/types/participant.types';
import { Store, StoreType } from '../../common/types/stores.types';
import { useStore } from '../../common/utils/use-store';
import { ComponentNames } from '../../components/types';
export class SlotService {
private isAssigningSlot = false;
public slot: Slot = {
index: null,
color: MEETING_COLORS.gray,
textColor: '#fff',
colorName: 'gray',
timestamp: Date.now(),
};
constructor(
private room: Socket.Room,
private useStore: <T extends StoreType>(name: T) => Store<T>,
) {
this.room = room;
this.room.presence.on(Socket.PresenceEvents.UPDATE, this.onPresenceUpdate);
}
/**
* @function assignSlot
* @description Assigns a slot to the participant
* @returns void
*/
public async assignSlot(): Promise<Slot> {
if (this.isAssigningSlot) return this.slot;
this.isAssigningSlot = true;
let slots = Array.from({ length: 50 }, (_, i) => i);
let slot = Math.floor(Math.random() * 50);
const { localParticipant, participants } = useStore(StoreType.GLOBAL);
try {
await new Promise((resolve, reject) => {
this.room.presence.get((presences) => {
if (!presences || !presences.length) resolve(true);
if (presences.length >= 50) {
slots = [];
reject(new Error('[SuperViz] - No more slots available'));
return;
}
presences.forEach((presence: Socket.PresenceEvent<Participant>) => {
if (presence.id === localParticipant.value.id) return;
slots = slots.filter((s) => s !== presence.data?.slot?.index);
});
resolve(true);
});
});
const isUsing = !slots.includes(slot);
if (isUsing) {
slot = slots.shift();
}
const color = Object.keys(MEETING_COLORS)[slot];
const slotData = {
index: slot,
color: MEETING_COLORS[color],
textColor: NAME_IS_WHITE_TEXT.includes(color) ? '#fff' : '#000',
colorName: color,
timestamp: Date.now(),
};
this.slot = slotData;
localParticipant.publish({
...localParticipant.value,
slot: slotData,
});
participants.publish({
...participants.value,
[localParticipant.value.id]: {
...participants.value[localParticipant.value.id],
slot: slotData,
},
});
this.room.presence.update({ slot: slotData });
this.isAssigningSlot = false;
return slotData;
} catch (error) {
console.error(error);
return null;
}
}
/**
* @function setDefaultSlot
* @description Removes the slot from the participant
* @returns void
*/
public setDefaultSlot() {
const { localParticipant, participants } = useStore(StoreType.GLOBAL);
const slot: Slot = {
index: null,
color: MEETING_COLORS.gray,
textColor: '#fff',
colorName: 'gray',
timestamp: Date.now(),
};
this.slot = slot;
localParticipant.publish({
...localParticipant.value,
slot: slot,
});
participants.publish({
...participants.value,
[localParticipant.value.id]: {
...participants.value[localParticipant.value.id],
slot,
},
});
this.room.presence.update({ slot });
}
private onPresenceUpdate = async (event: Socket.PresenceEvent<Participant>) => {
const { localParticipant } = this.useStore(StoreType.GLOBAL);
if (event.id === localParticipant.value.id) {
const slot = await this.validateSlotType(event.data);
localParticipant.publish({
...localParticipant.value,
slot: slot,
});
return;
}
if (event.data.slot?.index === null || this.slot.index === null) return;
if (event.data.slot?.index === this.slot?.index) {
const slotData = await this.assignSlot();
localParticipant.publish({
...localParticipant.value,
slot: slotData,
});
console.debug(
`[SuperViz] - Slot reassigned to ${localParticipant.value.id}, slot: ${slotData.colorName}`,
);
}
};
public participantNeedsSlot = (participant: Participant): boolean => {
const COMPONENTS_THAT_NEED_SLOT = [
ComponentNames.FORM_ELEMENTS,
ComponentNames.WHO_IS_ONLINE,
ComponentNames.PRESENCE,
ComponentNames.PRESENCE_AUTODESK,
ComponentNames.PRESENCE_MATTERPORT,
ComponentNames.PRESENCE_THREEJS,
];
const componentsNeedSlot = COMPONENTS_THAT_NEED_SLOT.some((component) => {
return participant?.activeComponents?.includes(component);
});
const videoNeedSlot =
participant?.activeComponents?.includes(ComponentNames.VIDEO_CONFERENCE) &&
participant.type !== ParticipantType.AUDIENCE;
const needSlot = componentsNeedSlot || videoNeedSlot;
return needSlot;
};
/**
* @function validateSlotType
* @description validate if the participant needs a slot
* @param {Participant} participant - new participant data
* @returns {void}
*/
private validateSlotType = async (participant: Participant): Promise<Slot> => {
if (this.isAssigningSlot) return this.slot;
const needSlot = this.participantNeedsSlot(participant);
if (participant.slot?.index === null && needSlot) {
const slotData = await this.assignSlot();
this.slot = slotData;
}
if (participant.slot?.index !== null && !needSlot) {
this.setDefaultSlot();
}
return this.slot;
};
}