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
221 lines (174 loc) · 6.89 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
import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '@superviz/socket-client';
import { throttle } from 'lodash';
import { Participant } from '../../common/types/participant.types';
import { StoreType } from '../../common/types/stores.types';
import { Logger, Observer } from '../../common/utils';
import { useStore } from '../../common/utils/use-store';
import { ParticipantDataInput, Presence3dEvents } from './types';
const SYNC_PROPERTY_INTERVAL = 1000;
export class Presence3DManager {
private room: Room;
private useStore: typeof useStore;
public participants3DObservers: Observer[] = [];
private localParticipant: Participant;
private logger: Logger;
constructor(room: Room, store: typeof useStore) {
this.room = room;
this.logger = new Logger('@superviz/sdk/presence3D-manager');
this.useStore = store;
this.subscribeToRoomEvents();
const { localParticipant } = this.useStore(StoreType.GLOBAL);
// have to set manually because useStore is binded to the 3d plugin that creates the service
localParticipant.subscribe((data) => {
this.room.presence.update(data);
const { participants } = this.useStore(StoreType.PRESENCE_3D);
const participant = {
...participants.value.find((participant) => participant.id === data.id),
...data,
};
participants.publish([
...participants.value.filter((participant) => participant.id !== data.id),
participant,
]);
this.localParticipant = participant;
});
}
private initializeParticipantsList = (): void => {
this.room.presence.get((presences) => {
presences.forEach((presence: PresenceEvent<Participant>) => {
this.unthrottledUpdatePresence3D(presence.data);
});
});
};
private onLocalParticipantJoined = (participant: Participant): void => {
if (!participant.slot || participant.slot?.index === null) {
setTimeout(() => {
this.onLocalParticipantJoined(this.localParticipant);
}, 2000);
return;
}
if (!this.room['isJoined']) {
setTimeout(() => {
this.onLocalParticipantJoined(participant);
}, 2000);
return;
}
const { hasJoined3D } = this.useStore(StoreType.PRESENCE_3D);
hasJoined3D.publish(true);
this.room.emit(Presence3dEvents.PARTICIPANT_JOINED, participant);
this.room.presence.update(participant);
this.initializeParticipantsList();
};
private subscribeToRoomEvents = (): void => {
this.room.on<Participant>(Presence3dEvents.PARTICIPANT_JOINED, this.onJoinedRoom);
this.room.presence.on(PresenceEvents.LEAVE, this.onLeaveRoom);
this.room.presence.on(PresenceEvents.UPDATE, this.onParticipantUpdate);
this.room.presence.on(PresenceEvents.JOINED_ROOM, this.onJoinedPresence);
};
private unsubscribeFromRoomEvents = (): void => {
this.room.off<Participant>(Presence3dEvents.PARTICIPANT_JOINED, this.onJoinedRoom);
this.room.presence.off(PresenceEvents.LEAVE);
this.room.presence.off(PresenceEvents.UPDATE);
this.room.presence.off(PresenceEvents.JOINED_ROOM);
};
private onJoinedRoom = (event: SocketEvent<Participant>): void => {
const { participants } = this.useStore(StoreType.PRESENCE_3D);
const participant = participants.value.find((participant) => participant.id === event.data.id);
participants.publish([
...participants.value.filter((participant) => participant.id !== event.data.id),
{
...participant,
...event.data,
},
]);
};
public onLeaveRoom = (event: PresenceEvent): void => {
if (event.id === this.localParticipant.id) {
this.unsubscribeFromRoomEvents();
this.useStore(StoreType.PRESENCE_3D).destroy();
return;
}
const { participants } = this.useStore(StoreType.PRESENCE_3D);
const updatedParticipants = participants.value.filter(
(participant) => participant.id !== event.id,
);
participants.publish(updatedParticipants);
};
private unthrottledUpdatePresence3D = (data: Participant): void => {
if (!data?.id) return;
const { participants, hasJoined3D } = this.useStore(StoreType.PRESENCE_3D);
const participant = {
...participants.value.find((participant) => participant.id === data.id),
...data,
};
participants.publish([
...participants.value.filter((participant) => participant.id !== data.id),
participant,
]);
if (!hasJoined3D.value || participant.id !== this.localParticipant.id) return;
this.room.presence.update(participant);
};
private onJoinedPresence = (event: PresenceEvent<Participant>): void => {
if (event.id !== this.localParticipant.id) return;
this.logger.log('participant joined 3D room', event.id, this.localParticipant);
this.onLocalParticipantJoined(this.localParticipant);
};
public updatePresence3D = throttle((data: Participant): void => {
this.unthrottledUpdatePresence3D(data);
}, SYNC_PROPERTY_INTERVAL);
/**
* @function publish3DUpdate
* @param {AblyParticipant} participant
* @description publish a participant's changes to observer
* @returns {void}
*/
private onParticipantUpdate = (event: PresenceEvent<Participant>): void => {
if (event.id === this.localParticipant.id) return;
const { participants } = this.useStore(StoreType.PRESENCE_3D);
const updatedParticipant = {
...participants.value.filter((participant) => participant.id === event.id)[0],
...event.data,
};
const updatedParticipants = participants.value.map((participant) => {
if (participant.id === event.id) {
return updatedParticipant;
}
return participant;
});
participants.publish(updatedParticipants);
if (this.participants3DObservers[event.id]) {
this.participants3DObservers[event.id].publish(updatedParticipant);
}
};
/**
* @function subscribeToUpdates
* @description subscribe to a participant's events
* @param {string} participantId
* @param {Function} callback
* @returns {void}
*/
public subscribeToUpdates(participantId: string, callback: Function): void {
if (!this.participants3DObservers[participantId]) {
this.participants3DObservers[participantId] = new Observer({ logger: this.logger });
}
this.participants3DObservers[participantId].subscribe(callback);
}
/**
* @function unsubscribeFromUpdates
* @description unsubscribe to a participant's events
* @param {string} participantId
* @param {Function} callback
* @returns {void}
*/
public unsubscribeFromUpdates(participantId: string, callback: Function): void {
if (this.participants3DObservers[participantId]) {
this.participants3DObservers[participantId].unsubscribe(callback);
}
}
public setParticipantData = (participant: ParticipantDataInput): void => {
this.updatePresence3D(participant);
};
public get getParticipants() {
return this.useStore(StoreType.PRESENCE_3D).participants.value;
}
}