From 5bf205195d307012c681c4781c61f07e0107377e Mon Sep 17 00:00:00 2001 From: Maxime Poulin Date: Tue, 16 Feb 2016 21:29:44 -0500 Subject: [PATCH] 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. --- client/js/lounge.js | 18 ++++++++++++++++++ src/client.js | 13 +++++++++++++ src/plugins/irc-events/join.js | 3 +-- src/plugins/irc-events/kick.js | 3 +-- src/plugins/irc-events/names.js | 3 +-- src/plugins/irc-events/nick.js | 3 +-- src/plugins/irc-events/part.js | 3 +-- src/plugins/irc-events/quit.js | 3 +-- src/server.js | 6 ++++++ 9 files changed, 43 insertions(+), 12 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 68956b9b..8d1af32a 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -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(); } diff --git a/src/client.js b/src/client.js index ade5102a..b074ef66 100644 --- a/src/client.js +++ b/src/client.js @@ -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] || []; diff --git a/src/plugins/irc-events/join.js b/src/plugins/irc-events/join.js index 23154f96..dbb51ef8 100644 --- a/src/plugins/irc-events/join.js +++ b/src/plugins/irc-events/join.js @@ -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()) { diff --git a/src/plugins/irc-events/kick.js b/src/plugins/irc-events/kick.js index a826450d..cc747928 100644 --- a/src/plugins/irc-events/kick.js +++ b/src/plugins/irc-events/kick.js @@ -19,8 +19,7 @@ module.exports = function(irc, network) { } client.emit("users", { - chan: chan.id, - users: chan.users + chan: chan.id }); var self = false; diff --git a/src/plugins/irc-events/names.js b/src/plugins/irc-events/names.js index 1fd2c1b0..50b2b3c4 100644 --- a/src/plugins/irc-events/names.js +++ b/src/plugins/irc-events/names.js @@ -14,8 +14,7 @@ module.exports = function(irc, network) { }); chan.sortUsers(); client.emit("users", { - chan: chan.id, - users: chan.users + chan: chan.id }); }); }; diff --git a/src/plugins/irc-events/nick.js b/src/plugins/irc-events/nick.js index fc65c4f2..d682902c 100644 --- a/src/plugins/irc-events/nick.js +++ b/src/plugins/irc-events/nick.js @@ -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, diff --git a/src/plugins/irc-events/part.js b/src/plugins/irc-events/part.js index d4c0bb1c..53069316 100644 --- a/src/plugins/irc-events/part.js +++ b/src/plugins/irc-events/part.js @@ -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) { diff --git a/src/plugins/irc-events/quit.js b/src/plugins/irc-events/quit.js index 46322a10..c448919e 100644 --- a/src/plugins/irc-events/quit.js +++ b/src/plugins/irc-events/quit.js @@ -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) { diff --git a/src/server.js b/src/server.js index fd1b29fd..06d0926a 100644 --- a/src/server.js +++ b/src/server.js @@ -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,