forked from jagvirdehal/spotify-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusermap.js
56 lines (46 loc) · 1.22 KB
/
usermap.js
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
class UserMap {
users = new Map() // userId -> userinfo object
sockets = new Map() // userId -> set of socketIds
add (user) {
this.users.set(user.id, user);
}
addSocket (userId, socketId) {
if (!this.sockets.has(userId)) {
this.sockets.set(userId, new Set());
}
this.sockets.get(userId).add(socketId);
}
remove (userId) {
this.users.delete(userId);
this.sockets.delete(userId);
}
removeSocket (userId, socketId) {
if (this.sockets.has(userId)) {
this.sockets.get(userId).delete(socketId);
}
}
get (userId) {
if (this.users.has(userId))
return this.users.get(userId);
else
return null
}
getRoom (userId) {
if (this.users.has(userId))
return this.users.get(userId).room
else
return null
}
getSockets (userId) {
if (this.sockets.has(userId))
return this.sockets.get(userId);
else
return null
}
setRoom (userId, room) {
if (this.users.has(userId)) {
this.users.get(userId).room = room;
}
}
};
module.exports = UserMap;