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
154 lines (130 loc) · 5.55 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
import { MeetingEvent } from '../../common/types/events.types';
import { Logger } from '../../common/utils';
import { AblyParticipant } from '../../services/realtime/ably/types';
import { BaseComponent } from '../base';
import { MouseOptions } from './types';
export class PresenceMouseComponent extends BaseComponent {
public name: string;
protected logger: Logger;
private presenceMouseElement: HTMLElement;
private containerId: string | null;
constructor(container?: string | null) {
super();
this.name = 'presence-mouse-component';
this.logger = new Logger('@superviz/sdk/presence-mouse-component');
this.containerId = container ?? null;
}
/**
* @function start
* @description start presence-mouse component
* @returns {void}
*/
protected start(): void {
this.logger.log('presence-mouse component @ start');
this.subscribeToRealtimeEvents();
}
/**
* @function destroy
* @description destroy presence-mouse component
* @returns {void}
*/
protected destroy(): void {
this.logger.log('presence-mouse component @ destroy');
this.publish(MeetingEvent.DESTROY);
this.unsubscribeFromRealtimeEvents();
const presenceContainerId = this.containerId ?
document.getElementById(this.containerId) : null;
if (presenceContainerId) {
presenceContainerId.removeEventListener('mousemove', this.onMyParticipantMouseMove);
presenceContainerId.removeChild(this.presenceMouseElement);
}
}
/**
* @function subscribeToRealtimeEvents
* @description subscribe to realtime events
* @returns {void}
*/
private subscribeToRealtimeEvents = (): void => {
this.logger.log('presence-mouse component @ subscribe to realtime events');
this.realtime.participantJoinedObserver.subscribe(this.onParticipantJoinedOnRealtime);
this.realtime.participantLeaveObserver.subscribe(this.onParticipantLeftOnRealtime);
this.realtime.participantsObserver.subscribe(this.onParticipantsDidChange);
};
/**
* @function unsubscribeFromRealtimeEvents
* @description subscribe to realtime events
* @returns {void}
*/
private unsubscribeFromRealtimeEvents = (): void => {
this.logger.log('presence-mouse component @ unsubscribe from realtime events');
this.realtime.participantJoinedObserver.unsubscribe(this.onParticipantJoinedOnRealtime);
this.realtime.participantLeaveObserver.unsubscribe(this.onParticipantLeftOnRealtime);
this.realtime.participantsObserver.unsubscribe(this.onParticipantsDidChange);
};
/** Presence Mouse Events */
/**
* @function onMyParticipantMouseMove
* @description event to update my participant mouse position to others participants
* @returns {void}
*/
private onMyParticipantMouseMove = (e): void => {
const presenceContainerId = this.containerId ?
document.getElementById(this.containerId) : document?.body;
const rect = presenceContainerId.getBoundingClientRect();
this.realtime.updateMyProperties({
mousePositionX: this.containerId ? e.x - rect.x : e.x,
mousePositionY: this.containerId ? e.y - rect.y : e.y,
originalWidth: this.containerId ? rect.width : 1,
originalHeight: this.containerId ? rect.height : 1,
containerId: this.containerId,
});
};
/**
* @function onParticipantsDidChange
* @description handler for participant list update event
* @param {Record<string, AblyParticipant>} participants - participants
* @returns {void}
*/
private onParticipantsDidChange = (participants: Record<string, AblyParticipant>): void => {
this.logger.log('presence-mouse component @ on participants did change', participants);
Object.values(participants).forEach((participant: AblyParticipant) => {
const externalParticipantData: MouseOptions = participant.data;
const hasPresenceMouseElement = externalParticipantData?.mousePositionX
&& this.presenceMouseElement;
const myParticipant = externalParticipantData?.id === this.localParticipant?.id;
externalParticipantData.color = this.realtime.getSlotColor(participant.data.slotIndex).color;
externalParticipantData.slotIndex = participant.data.slotIndex;
if (!myParticipant && hasPresenceMouseElement) {
// @ts-ignore
this.presenceMouseElement.updatePresenceMouseParticipant(externalParticipantData);
}
});
};
/**
* @function onParticipantJoinedOnRealtime
* @description handler for participant joined event
* @param {AblyParticipant} participant - participant
* @returns {void}
*/
private onParticipantJoinedOnRealtime = (participant: AblyParticipant): void => {
this.logger.log('presence-mouse component @ on participant joined on realtime', participant);
if (participant?.data?.id === this.localParticipant?.id) {
const presenceContainerId = this.containerId ?
document.getElementById(this.containerId) : document?.body;
this.presenceMouseElement = document.createElement('superviz-presence-mouse');
presenceContainerId.appendChild(this.presenceMouseElement);
presenceContainerId.addEventListener('mousemove', this.onMyParticipantMouseMove);
}
};
/**
* @function onParticipantLeftOnRealtime
* @description handler for participant left event
* @param {AblyParticipant} participant
* @returns {void}
*/
private onParticipantLeftOnRealtime = (participant: AblyParticipant): void => {
this.logger.log('presence-mouse component @ on participant left on realtime', participant);
// @ts-ignore
this.presenceMouseElement.removePresenceMouseParticipant(participant.clientId);
};
}