From a52742876348210b44988ae3ac595d185d1925f1 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Fri, 29 Jul 2016 14:12:46 +0200 Subject: [PATCH] Periodic hotlist sync. Fixes #692 There is a bug in WeeChat when hdata is null, which happens when the hotlist is empty (meaning all buffers are read). Our websocket callback machinery expects every command with id to return data, so this code in current versions of WeeChat will lead to a slow leak. But I think lots of things in our code will do this already so I'm not entirely sure it's too problematic to let this patch go by. We could use infolists instead of hdata, but that is cumbersome to parse and less performant for WeeChat, and sends more data over the wire. I propose we make a separate attempt at cleaning up the callbacks. Since we store time on them we could have a cleanuptask that looks through them and deletes old callbacks. Maybe @dcormier could have a look? --- js/connection.js | 22 +++++++++++++++++----- js/handlers.js | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/js/connection.js b/js/connection.js index 00e4b6d..e20a29e 100644 --- a/js/connection.js +++ b/js/connection.js @@ -151,21 +151,33 @@ weechat.factory('connection', _requestHotlist().then(function(hotlist) { handlers.handleHotlistInfo(hotlist); - if (successCallback) { - successCallback(); - } }); + // Schedule hotlist syncing every so often so that this + // client will have unread counts (mostly) in sync with + // other clients or terminal usage directly. + setInterval(function() { + if ($rootScope.connected) { + _requestHotlist().then(function(hotlist) { + handlers.handleHotlistInfo(hotlist); + + }); + } + }, 60000); // Sync hotlist every 60 second + // Fetch weechat time format for displaying timestamps fetchConfValue('weechat.look.buffer_time_format', function() { + // Will set models.wconfig['weechat.look.buffer_time_format'] _parseWeechatTimeFormat(); - }); - // Will set models.wconfig['weechat.look.buffer_time_format'] + }); _requestSync(); $log.info("Connected to relay"); $rootScope.connected = true; + if (successCallback) { + successCallback(); + } }, function() { handleWrongPassword(); diff --git a/js/handlers.js b/js/handlers.js index c1b3228..38196ad 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -340,23 +340,33 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific * Handle answers to hotlist request */ var handleHotlistInfo = function(message) { - if (message.objects.length === 0) { - return; + // Hotlist includes only buffers with unread counts so first we + // iterate all our buffers and resets the counts. + _.each(models.getBuffers(), function(buffer) { + buffer.unread = 0; + buffer.notification = 0; + }); + if (message.objects.length > 0) { + var hotlist = message.objects[0].content; + hotlist.forEach(function(l) { + var buffer = models.getBuffer(l.buffer); + // 1 is message + buffer.unread = l.count[1]; + // 2 is private + buffer.notification = l.count[2]; + // 3 is highlight + buffer.notification = l.count[3]; + /* Since there is unread messages, we can guess + * what the last read line is and update it accordingly + */ + var unreadSum = _.reduce(l.count, function(memo, num) { return memo + num; }, 0); + buffer.lastSeen = buffer.lines.length - 1 - unreadSum; + }); } - var hotlist = message.objects[0].content; - hotlist.forEach(function(l) { - var buffer = models.getBuffer(l.buffer); - // 1 is message - buffer.unread += l.count[1]; - // 2 is private - buffer.notification += l.count[2]; - // 3 is highlight - buffer.notification += l.count[3]; - /* Since there is unread messages, we can guess - * what the last read line is and update it accordingly - */ - var unreadSum = _.reduce(l.count, function(memo, num) { return memo + num; }, 0); - buffer.lastSeen = buffer.lines.length - 1 - unreadSum; + // the unread badges in the bufferlist doesn't update if we don't do this + setTimeout(function() { + $rootScope.$apply(); + $rootScope.$emit('notificationChanged'); }); };