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
256 lines (222 loc) · 8.57 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
import { isEqual } from 'lodash';
import { RealtimeEvent } from '../../common/types/events.types';
import { Logger } from '../../common/utils';
import { AblyParticipant } from '../../services/realtime/ably/types';
import { WhoIsOnline as WhoIsOnlineElement } from '../../web-components';
import { BaseComponent } from '../base';
import { ComponentNames } from '../types';
import { WhoIsOnlinePosition, Position, Participant } from './types';
export class WhoIsOnline extends BaseComponent {
public name: ComponentNames;
protected logger: Logger;
private element: WhoIsOnlineElement;
private position: WhoIsOnlinePosition;
private participants: Participant[] = [];
private following: string;
constructor(position?: WhoIsOnlinePosition) {
super();
this.position = position ?? Position.TOP_RIGHT;
this.name = ComponentNames.WHO_IS_ONLINE;
this.logger = new Logger('@superviz/sdk/who-is-online-component');
}
/**
* @function start
* @description Initializes the Who Is Online component
* @returns {void}
*/
protected start(): void {
this.subscribeToRealtimeEvents();
this.positionWhoIsOnline();
this.addListeners();
this.realtime.enterWIOChannel(this.localParticipant);
}
/**
* @function destroy
* @description Destroys the Who Is Online component
* @returns {void}
*/
protected destroy(): void {
this.unsubscribeToRealtimeEvents();
this.realtime.leaveWIOChannel();
this.removeListeners();
this.element.remove();
this.element = null;
this.participants = null;
}
/**
* @function addListeners
* @description adds event listeners to the who is online element.
* @returns {void}
*/
private addListeners(): void {
this.element.addEventListener(
RealtimeEvent.REALTIME_LOCAL_FOLLOW_PARTICIPANT,
this.followMousePointer,
);
this.element.addEventListener(RealtimeEvent.REALTIME_GO_TO_PARTICIPANT, this.goToMousePointer);
this.element.addEventListener(RealtimeEvent.REALTIME_PRIVATE_MODE, this.setPrivate);
this.element.addEventListener(RealtimeEvent.REALTIME_FOLLOW_PARTICIPANT, this.follow);
this.element.addEventListener(RealtimeEvent.REALTIME_GATHER, this.gather);
}
/**
* @function removeListeners
* @description adds event listeners from the who is online element.
* @returns {void}
*/
private removeListeners(): void {
this.element.removeEventListener(
RealtimeEvent.REALTIME_LOCAL_FOLLOW_PARTICIPANT,
this.followMousePointer,
);
this.element.removeEventListener(
RealtimeEvent.REALTIME_GO_TO_PARTICIPANT,
this.goToMousePointer,
);
this.element.removeEventListener(RealtimeEvent.REALTIME_PRIVATE_MODE, this.setPrivate);
this.element.removeEventListener(RealtimeEvent.REALTIME_FOLLOW_PARTICIPANT, this.follow);
this.element.removeEventListener(RealtimeEvent.REALTIME_GATHER, this.gather);
}
/**
* @function subscribeToRealtimeEvents
* @description Subscribes to realtime events
* @returns {void}
*/
private subscribeToRealtimeEvents(): void {
this.realtime.participantsObserver.subscribe(this.onParticipantListUpdate);
this.realtime.participantLeaveObserver.subscribe(this.stopFollowing);
this.realtime.privateModeWIOObserver.subscribe(this.onParticipantListUpdate);
this.realtime.followWIOObserver.subscribe(this.setFollow);
this.realtime.gatherWIOObserver.subscribe(this.goToMousePointer);
}
/**
* @function unsubscribeToRealtimeEvents
* @description Unsubscribes to realtime events
* @returns {void}
*/
private unsubscribeToRealtimeEvents(): void {
this.realtime.participantsObserver.unsubscribe(this.onParticipantListUpdate);
this.realtime.participantLeaveObserver.unsubscribe(this.stopFollowing);
this.realtime.privateModeWIOObserver.unsubscribe(this.onParticipantListUpdate);
this.realtime.followWIOObserver.unsubscribe(this.setFollow);
this.realtime.gatherWIOObserver.unsubscribe(this.goToMousePointer);
}
/**
* @function onParticipantListUpdate
* @description Receives data about participants in the room who were not loaded
* when the component was initialized
* @param {Record<string, AblyParticipant>} data
* @returns {void}
*/
private onParticipantListUpdate = (data: Record<string, AblyParticipant>): void => {
const updatedParticipants = Object.values(data).filter(({ data }) => {
return data.activeComponents?.includes('whoIsOnline');
});
const participants = updatedParticipants
.filter(({ data: { isPrivate, id } }) => {
return !isPrivate || (isPrivate && id === this.localParticipant.id);
})
.map(({ data }) => {
const { slotIndex, id, name, avatar, activeComponents } = data as Participant;
const { color } = this.realtime.getSlotColor(slotIndex);
const isLocal = this.localParticipant.id === id;
const joinedPresence = activeComponents.some((component) => component.includes('presence'));
this.setLocalData(isLocal, !joinedPresence, color, joinedPresence);
return { name, id, slotIndex, color, isLocal, joinedPresence, avatar };
});
if (isEqual(participants, this.participants)) return;
if (this.following) {
const participantBeingFollowed = participants.find(({ id }) => id === this.following);
if (!participantBeingFollowed) this.stopFollowing({ clientId: this.following });
}
this.participants = participants;
this.element.updateParticipants(this.participants);
};
private setLocalData = (
local: boolean,
disable: boolean,
color: string,
joinedPresence: boolean,
) => {
if (!local) return;
this.element.disableDropdown = disable;
this.element.localParticipantData = { color, id: this.localParticipant.id, joinedPresence };
};
/**
* @function positionWhoIsOnline
* @description Positions the Who Is Online component on the screen
* @returns {void}
*/
private positionWhoIsOnline(): void {
this.element = document.createElement('superviz-who-is-online') as WhoIsOnlineElement;
const isUsingDefaultPosition = Object.values(Position).includes(
this.position.toLowerCase() as Position,
);
if (isUsingDefaultPosition) {
document.body.appendChild(this.element);
const [vertical, horizontal] = this.position.split('-');
this.element.position = `${vertical}: 20px; ${horizontal}: 40px;`;
return;
}
const container = document.getElementById(this.position);
if (!container) {
this.element.position = 'top: 20px; right: 40px;';
document.body.appendChild(this.element);
return;
}
container.appendChild(this.element);
this.element.position = 'position: relative;';
}
/**
* @function goToMousePointer
* @description Publishes the go to event to the event bus
* @param {CustomEvent} event
* @returns {void}
*/
private goToMousePointer = ({ detail }: CustomEvent) => {
this.eventBus.publish(RealtimeEvent.REALTIME_GO_TO_PARTICIPANT, detail.id);
};
/**
* @function followMousePointer
* @description Publishes the follow event to the event bus
* @param {CustomEvent} event
* @returns {void}
*/
private followMousePointer = ({ detail }: CustomEvent) => {
this.eventBus.publish(RealtimeEvent.REALTIME_LOCAL_FOLLOW_PARTICIPANT, detail.id);
this.following = detail.id;
};
/**
* @function setPrivate
* @description Publishes the private event to realtime and the event bus
* @param {CustomEvent} event
* @returns {void}
*/
private setPrivate = ({ detail: { isPrivate, id } }: CustomEvent) => {
this.eventBus.publish(RealtimeEvent.REALTIME_PRIVATE_MODE, isPrivate);
this.realtime.setPrivateWIOParticipant(id, isPrivate);
};
private setFollow = (following) => {
if (following.clientId === this.localParticipant.id) return;
this.followMousePointer({ detail: { id: following?.data?.id } } as CustomEvent);
if (!following.data.id) {
this.element.following = undefined;
return;
}
this.following = following.data.id;
this.element.following = following.data;
};
private follow = (data: CustomEvent) => {
this.realtime.setFollowWIOParticipant({ ...data.detail });
this.following = data.detail?.id;
};
private stopFollowing = (participant: { clientId: string }) => {
if (participant.clientId === this.element.following?.id) {
this.element.following = undefined;
this.following = undefined;
this.eventBus.publish(RealtimeEvent.REALTIME_LOCAL_FOLLOW_PARTICIPANT, undefined);
}
};
private gather = (data: CustomEvent) => {
this.realtime.setGatherWIOParticipant({ ...data.detail });
};
}