-
Notifications
You must be signed in to change notification settings - Fork 2
/
user-registry.js
51 lines (42 loc) · 1.19 KB
/
user-registry.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
/**
* Created by eak on 9/14/15.
*/
function UserRegistry() {
this.usersById = {};//유저의 session이 저장됨
this.usersByName = {};
}
UserRegistry.prototype.register = function (user) {
this.usersById[user.id] = user;
this.usersByName[user.name] = user;
};
UserRegistry.prototype.unregister = function (id) {
var user = this.getById(id);
if (user)
delete this.usersById[id];
if (user && this.getByName(user.name))
delete this.usersByName[user.name]
};
UserRegistry.prototype.getById = function (id) {
return this.usersById[id];
};
UserRegistry.prototype.getByName = function (name) {
return this.usersByName[name];
};
UserRegistry.prototype.removeById = function (id) {
var userSession = this.usersById[id];
if (!userSession)
return;
delete this.usersById[id];
delete this.usersByName[userSession.name];
};
UserRegistry.prototype.getUsersByRoom = function (room) {
var userList = this.usersByName;
var usersInRoomList = [];
for (var i in userList) {
if (userList[i].room === room) {
usersInRoomList.push(userList[i]);
}
}
return usersInRoomList;
};
module.exports = UserRegistry;