[polari/wip/fmuellner/window-experiments: 7/30] chatView: Split out helper function for clarity



commit d7c95fb48f09c3954603b46f85a9c132fefc716a
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Jul 16 05:19:34 2016 +0200

    chatView: Split out helper function for clarity
    
    When we display consecutive messages from the same sender in the
    chat log, we only insert the nick for the first one. This is trivial
    for regular messages we append to the end, but a bit trickier for
    log messages where we move backwards. To get the grouping right,
    we don't insert all retrieved logs immediately, but only from the
    first message that groups differently than the oldest message we
    currently have from logs. This works fine, but having the code
    directly in insertPendingLogs() distracts a bit from the main
    functionality, plus a separate function can be simplified by
    using returns instead of nesting if blocks.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=768907

 src/chatView.js |   29 +++++++++++++----------------
 1 files changed, 13 insertions(+), 16 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index ade250b..9479ff5 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -498,30 +498,27 @@ const ChatView = new Lang.Class({
         throw new Error('Cannot create message from source ' + source);
     },
 
-    _insertPendingLogs: function() {
-        if (this._pendingLogs.length == 0)
-            return;
+    _getReadyLogs: function() {
+        if (this._logWalker.is_end())
+            return this._pendingLogs.splice(0);
 
-        let index = -1;
         let nick = this._pendingLogs[0].nick;
         let type = this._pendingLogs[0].messageType;
-        if (!this._logWalker.is_end()) {
-            for (let i = 0; i < this._pendingLogs.length; i++)
-                if (this._pendingLogs[i].nick != nick ||
-                    this._pendingLogs[i].messageType != type) {
-                    index = i;
-                    break;
-                }
-        } else {
-            index = 0;
-        }
+        for (let i = 0; i < this._pendingLogs.length; i++)
+            if (this._pendingLogs[i].nick != nick ||
+                this._pendingLogs[i].messageType != type)
+                return this._pendingLogs.splice(i);
+        return [];
+    },
+
+    _insertPendingLogs: function() {
+        let pending = this._getReadyLogs();
 
-        if (index < 0) {
+        if (!pending.length) {
             this._fetchBacklog();
             return;
         }
 
-        let pending = this._pendingLogs.splice(index);
         let state = { lastNick: null, lastTimestamp: 0 };
         let iter = this._view.buffer.get_start_iter();
         for (let i = 0; i < pending.length; i++) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]