-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
SSRCMap.ts
108 lines (92 loc) · 2.34 KB
/
SSRCMap.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
import { EventEmitter } from 'node:events';
/**
* The known data for a user in a Discord voice connection.
*/
export interface VoiceUserData {
/**
* The SSRC of the user's audio stream.
*/
audioSSRC: number;
/**
* The Discord user id of the user.
*/
userId: string;
/**
* The SSRC of the user's video stream (if one exists)
* Cannot be 0. If undefined, the user has no video stream.
*/
videoSSRC?: number;
}
export interface SSRCMap extends EventEmitter {
on(event: 'create', listener: (newData: VoiceUserData) => void): this;
on(event: 'update', listener: (oldData: VoiceUserData | undefined, newData: VoiceUserData) => void): this;
on(event: 'delete', listener: (deletedData: VoiceUserData) => void): this;
}
/**
* Maps audio SSRCs to data of users in voice connections.
*/
export class SSRCMap extends EventEmitter {
/**
* The underlying map.
*/
private readonly map: Map<number, VoiceUserData>;
public constructor() {
super();
this.map = new Map();
}
/**
* Updates the map with new user data
*
* @param data - The data to update with
*/
public update(data: VoiceUserData) {
const existing = this.map.get(data.audioSSRC);
const newValue = {
...this.map.get(data.audioSSRC),
...data,
};
this.map.set(data.audioSSRC, newValue);
if (!existing) this.emit('create', newValue);
this.emit('update', existing, newValue);
}
/**
* Gets the stored voice data of a user.
*
* @param target - The target, either their user id or audio SSRC
*/
public get(target: number | string) {
if (typeof target === 'number') {
return this.map.get(target);
}
for (const data of this.map.values()) {
if (data.userId === target) {
return data;
}
}
return undefined;
}
/**
* Deletes the stored voice data about a user.
*
* @param target - The target of the delete operation, either their audio SSRC or user id
* @returns The data that was deleted, if any
*/
public delete(target: number | string) {
if (typeof target === 'number') {
const existing = this.map.get(target);
if (existing) {
this.map.delete(target);
this.emit('delete', existing);
}
return existing;
}
for (const [audioSSRC, data] of this.map.entries()) {
if (data.userId === target) {
this.map.delete(audioSSRC);
this.emit('delete', data);
return data;
}
}
return undefined;
}
}