[polari/wip/fmuellner/window-experiments: 6/11] roomList: Use Map to keep track of rows/placeholders



commit 841ff6831050d19ac7c6c2f369c752da84c02259
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Jul 18 21:03:11 2016 +0200

    roomList: Use Map to keep track of rows/placeholders
    
    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 ...

 src/roomList.js |   57 +++++++++++++++++++++++++-----------------------------
 1 files changed, 26 insertions(+), 31 deletions(-)
---
diff --git a/src/roomList.js b/src/roomList.js
index 51172d5..95ae9c2 100644
--- a/src/roomList.js
+++ b/src/roomList.js
@@ -335,8 +335,8 @@ const RoomList = new Lang.Class({
         this.set_header_func(Lang.bind(this, this._updateHeader));
         this.set_sort_func(Lang.bind(this, this._sort));
 
-        this._placeholders = {};
-        this._roomRows = {};
+        this._placeholders = new Map();
+        this._roomRows = new Map();
 
         this._accountsMonitor = AccountsMonitor.getDefault();
         let feature = Tp.AccountManager.get_feature_quark_core();
@@ -414,18 +414,16 @@ const RoomList = new Lang.Class({
 
     _onLeaveActivated: function(action, param) {
         let [id, ] = param.deep_unpack();
-        let row = this._roomRows[id];
+        let row = this._roomRows.get(id);
 
         this._moveSelectionFromRow(row);
         row.hide();
     },
 
     _rowToRoomIndex: function(index) {
-        let nPlaceholders = Object.keys(this._placeholders).filter(
-            Lang.bind(this, function(a) {
-                return this._placeholders[a].get_index() < index;
-            })).length;
-        return index - nPlaceholders;
+        let placeholders = [...this._placeholders.values()];
+        let nBefore = placeholders.filter(p => p.get_index()).length;
+        return index - nBefore;
     },
 
     _roomToRowIndex: function(index) {
@@ -474,7 +472,7 @@ const RoomList = new Lang.Class({
             return;
 
         let toplevel = this.get_toplevel();
-        let current = this._roomRows[toplevel.active_room.id];
+        let current = this._roomRows.get(toplevel.active_room.id);
 
         if (current != row)
             return;
@@ -497,14 +495,14 @@ const RoomList = new Lang.Class({
     },
 
     _accountAdded: function(am, account) {
-        if (this._placeholders[account])
+        if (this._placeholders.has(account))
             return;
 
         let placeholder = new Gtk.ListBoxRow({ selectable: false,
                                                activatable: false });
         placeholder.account = account;
 
-        this._placeholders[account] = placeholder;
+        this._placeholders.set(account, placeholder);
         this.add(placeholder);
 
         placeholder.connect('notify::visible', Lang.bind(this,
@@ -516,57 +514,54 @@ const RoomList = new Lang.Class({
     },
 
     _accountRemoved: function(am, account) {
-        let placeholder = this._placeholders[account];
+        let placeholder = this._placeholders.get(account);
 
         if (!placeholder)
             return;
 
-        delete this._placeholders[account];
+        this._placeholders.delete(account);
         placeholder.destroy();
     },
 
     _roomAdded: function(roomManager, room) {
+        if (this._roomRows.has(room.id))
+            return;
+
         let row = new RoomRow(room);
         this.add(row);
-        this._roomRows[room.id] = row;
+        this._roomRows.set(room.id, row);
 
-        row.connect('destroy', Lang.bind(this,
-            function(w) {
-                delete this._roomRows[w.room.id];
-            }));
-        this._placeholders[room.account].hide();
+        row.connect('destroy', w => { this._roomRows.delete(w.room.id); });
+        this._placeholders.get(room.account).hide();
     },
 
     _roomRemoved: function(roomManager, room) {
-        let row = this._roomRows[room.id];
+        let row = this._roomRows.get(room.id);
         if (!row)
             return;
 
         this._moveSelectionFromRow(row);
         row.destroy();
-        delete this._roomRows[room.id];
+        this._roomRows.delete(room.id);
         this._updatePlaceholderVisibility(room.account);
     },
 
     _updatePlaceholderVisibility: function(account) {
         if (!account.enabled) {
-            this._placeholders[account].hide();
+            this._placeholders.get(account).hide();
             return;
         }
 
-        let ids = Object.keys(this._roomRows);
-        let hasRooms = ids.some(Lang.bind(this,
-            function(id) {
-                return this._roomRows[id].account == account;
-            }));
-        this._placeholders[account].visible = !hasRooms;
+        let rows = [...this._roomRows.values()];
+        let hasRooms = rows.some(r => r.account == account);
+        this._placeholders.get(account).visible = !hasRooms;
     },
 
     _activeRoomChanged: function() {
         let room = this.get_toplevel().active_room;
         if (!room)
             return;
-        let row = this._roomRows[room.id];
+        let row = this._roomRows.get(room.id);
         if (!row)
             return;
 
@@ -607,8 +602,8 @@ const RoomList = new Lang.Class({
         let account1 = row1.account;
         let account2 = row2.account;
 
-        let hasRooms1 = !this._placeholders[account1].visible;
-        let hasRooms2 = !this._placeholders[account2].visible;
+        let hasRooms1 = !this._placeholders.get(account1).visible;
+        let hasRooms2 = !this._placeholders.get(account2).visible;
 
         if (hasRooms1 != hasRooms2)
             return hasRooms1 ? -1 : 1;


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