[polari/wip/fmuellner/window-experiments: 2/30] chatView: Store pending marks in a map



commit 56335734a7da9c76eebd672cff8c705c1fd473fa
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Jul 15 18:43:46 2016 +0200

    chatView: Store pending marks in a map
    
    With the availability of the ECMA6 Map class in gjs, it's time to
    phase out the old pattern of using objects as hash tables ...
    
    https://bugzilla.gnome.org/show_bug.cgi?id=768907

 src/chatView.js |   24 +++++++++---------------
 1 files changed, 9 insertions(+), 15 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index dabd478..218a8c1 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -296,7 +296,7 @@ const ChatView = new Lang.Class({
         this._maxNickChars = MAX_NICK_CHARS;
         this._hoveredButtonTags = [];
         this._needsIndicator = true;
-        this._pending = {};
+        this._pending = new Map();
         this._pendingLogs = [];
         this._statusCount = { left: 0, joined: 0, total: 0 };
 
@@ -527,10 +527,6 @@ const ChatView = new Lang.Class({
         }
     },
 
-    get _nPending() {
-        return Object.keys(this._pending).length;
-    },
-
     get max_nick_chars() {
         return this._maxNickChars;
     },
@@ -585,15 +581,13 @@ const ChatView = new Lang.Class({
         if (!this._autoscroll)
             return;
 
-        if (this._nPending == 0) {
+        if (this._pending.size == 0) {
             this._view.emit('move-cursor',
                             Gtk.MovementStep.BUFFER_ENDS, 1, false);
         } else {
-            let id = Object.keys(this._pending).sort(function(a, b) {
-                return a - b;
-            })[0];
             this._autoscroll = false;
-            this._view.scroll_mark_onscreen(this._pending[id]);
+            let mark = [...this._pending.values()].shift();
+            this._view.scroll_mark_onscreen(mark);
         }
     },
 
@@ -672,7 +666,7 @@ const ChatView = new Lang.Class({
 
     _pendingMessageRemoved: function(channel, message) {
         let [id,] = message.get_pending_message_id();
-        let mark = this._pending[id];
+        let mark = this._pending.get(id);
         if (!mark)
             return;
         // Re-enable auto-scrolling if this is the most recent message
@@ -680,7 +674,7 @@ const ChatView = new Lang.Class({
             this._autoscroll = true;
         this._view.buffer.delete_mark(mark);
         this._app.withdraw_notification('pending-message-' + id);
-        delete this._pending[id];
+        this._pending.delete(id);
     },
 
     _showUrlContextMenu: function(url, button, time) {
@@ -781,7 +775,7 @@ const ChatView = new Lang.Class({
         let buffer = this._view.get_buffer();
         for (let i = 0; i < pending.length; i++) {
             let [id,] = pending[i].get_pending_message_id();
-            let mark = this._pending[id];
+            let mark = this._pending.get(id);
             if (!mark) {
                 this._channel.ack_message_async(pending[i], null);
                 continue;
@@ -1186,14 +1180,14 @@ const ChatView = new Lang.Class({
 
         let buffer = this._view.get_buffer();
         if (!valid /* outgoing */ ||
-            (this._active && this._toplevelFocus && this._nPending == 0)) {
+            (this._active && this._toplevelFocus && this._pending.size == 0)) {
             this._channel.ack_message_async(tpMessage, null);
         } else if (shouldHighlight || this._needsIndicator) {
             let iter = buffer.get_end_iter();
 
             if (shouldHighlight) {
                 let mark = buffer.create_mark(null, iter, true);
-                this._pending[id] = mark;
+                this._pending.set(id, mark);
             }
 
             if (this._needsIndicator) {


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