Only update the users list when needed

Currently, for join/part/kick/nick/... the server will send an updated list of users and the client will re-render the list entirely. This ends up being a very expensive operation when joined on large channels and causes the client to slow down a lot.
This commit is contained in:
Maxime Poulin 2016-02-16 21:29:44 -05:00
parent 0d823f24d6
commit 5bf205195d
9 changed files with 43 additions and 12 deletions

View File

@ -394,6 +394,19 @@ $(function() {
});
socket.on("users", function(data) {
var chan = chat.find("#chan-" + data.chan);
if (chan.hasClass("active")) {
socket.emit("names", {
target: data.chan
});
}
else {
chan.data("needsNamesRefresh", true);
}
});
socket.on("names", function(data) {
var users = chat.find("#chan-" + data.chan).find(".users").html(render("user", data));
var nicks = [];
for (var i in data.users) {
@ -572,6 +585,11 @@ $(function() {
}
}
if (chan.data("needsNamesRefresh") === true) {
chan.data("needsNamesRefresh", false);
socket.emit("names", {target: self.data("id")});
}
if (screen.width > 768 && chan.hasClass("chan")) {
input.focus();
}

View File

@ -303,6 +303,19 @@ Client.prototype.sort = function(data) {
}
};
Client.prototype.names = function(data) {
var client = this;
var target = client.find(data.target);
if (!target) {
return;
}
client.emit("names", {
chan: target.chan.id,
users: target.chan.users
});
};
Client.prototype.quit = function() {
var sockets = this.sockets.sockets;
var room = sockets.adapter.rooms[this.id] || [];

View File

@ -21,8 +21,7 @@ module.exports = function(irc, network) {
chan.users.push(new User({name: data.nick}));
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
var self = false;
if (data.nick.toLowerCase() === irc.me.toLowerCase()) {

View File

@ -19,8 +19,7 @@ module.exports = function(irc, network) {
}
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
var self = false;

View File

@ -14,8 +14,7 @@ module.exports = function(irc, network) {
});
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
});
};

View File

@ -31,8 +31,7 @@ module.exports = function(irc, network) {
user.name = nick;
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
var msg = new Msg({
type: Msg.Type.NICK,

View File

@ -19,8 +19,7 @@ module.exports = function(irc, network) {
var user = _.findWhere(chan.users, {name: from});
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
var reason = data.message || "";
if (reason.length > 0) {

View File

@ -12,8 +12,7 @@ module.exports = function(irc, network) {
}
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id,
users: chan.users
chan: chan.id
});
var reason = data.message || "";
if (reason.length > 0) {

View File

@ -121,6 +121,12 @@ function init(socket, client, token) {
client.sort(data);
}
);
socket.on(
"names",
function(data) {
client.names(data);
}
);
socket.join(client.id);
socket.emit("init", {
active: client.activeChannel,