Skip to content

Commit 0d27217

Browse files
Use an iterable (Map) insted of dictionary (Object) to keep model of rooms and users.
1 parent 30eb70c commit 0d27217

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lobby.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
const version = "v1_0_7";
1+
const version = "v1_0_11";
22

33
class LobbyModel extends Croquet.Model {
44
init(options = {}) {
55
super.init(options);
6-
this.users = {}; // lobby session user id => room id, so that everyone can lower that count in the lobby when they go offline
7-
this.rooms = {}; // room name => concurrency count, for display (and for removing when it goes to zero)
6+
this.users = new Map(); // lobby session user id => room id, so that everyone can lower that count in the lobby when they go offline
7+
this.rooms = new Map(); // room name => concurrency count, for display (and for removing when it goes to zero)
88
this.subscribe(this.sessionId, 'view-join', this.join);
99
this.subscribe(this.sessionId, 'view-exit', this.exit);
1010
this.subscribe(this.sessionId, 'updateModel', this.updateModel);
1111
}
1212
updateModel({userId, roomId}) {
13-
const oldRoomId = this.users[userId];
13+
const oldRoomId = this.users.get(userId);
1414
if (oldRoomId) {
15-
if (--this.rooms[oldRoomId] <= 0) {
16-
delete this.rooms[oldRoomId];
15+
if (this.adjustRoom(oldRoomId, -1) <= 0) {
16+
this.rooms.delete(oldRoomId);
1717
}
1818
}
1919
if (roomId) {
20-
this.users[userId] = roomId;
21-
this.rooms[roomId] = (this.rooms[roomId] || 0) + 1;
20+
this.users.set(userId, roomId);
21+
this.adjustRoom(roomId, 1);
2222
} else { // Back to lobby.
23-
delete this.users[userId]; // Clean up users dictionary.
23+
this.users.delete(userId); // Clean up users dictionary.
2424
}
2525
this.publish(this.sessionId, 'updateDisplay');
2626
}
27+
adjustRoom(roomId, increment = 1) {
28+
let count = this.rooms.get(roomId) || 0;
29+
count += increment;
30+
this.rooms.set(roomId, count);
31+
return count;
32+
}
2733
join(userId) { // Everyone enters through Lobby.
2834
this.updateModel({userId});
2935
}
@@ -42,15 +48,11 @@ class LobbyUI extends Croquet.View {
4248
makeRoomButton.onclick = () => this.enterRoom(newRoomName.value); // No need to make it, just enter.
4349
}
4450
updateDisplay() {
45-
const templateContent = roomListTemplate.content,
46-
rooms = this.model.rooms,
47-
names = Object.keys(rooms).reverse(); // Let's list the newest first.
51+
const templateContent = roomListTemplate.content;
4852
while (roomList.firstChild) { // roomList.innerHTML = '' would not remove event handlers.
4953
roomList.removeChild(roomList.firstChild);
5054
}
51-
for (let name of names) { // TODO: keep this stable instead of flashing during changes. (Add or remove only as needed, and update concurrency.)
52-
let concurrency = rooms[name];
53-
console.log(name, concurrency);
55+
for (let [name, concurrency] of this.model.rooms) { // TODO: keep this stable instead of flashing during changes. (Add or remove only as needed, and update concurrency.)
5456
if (!concurrency) break;
5557
let item = templateContent.cloneNode(true).firstElementChild,
5658
link = item.getElementsByTagName('a')[0];

0 commit comments

Comments
 (0)