[polari/wip/fmuellner/cleanups: 3/5] cleanup: Replace more plain objects with Maps



commit 6774bac8eb6e321e04d3bdb660daf9f2f57d4b05
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Nov 4 23:39:02 2017 +0100

    cleanup: Replace more plain objects with Maps
    
    While we replaced most uses of the objects-as-hash-tables pattern a
    while ago, there are still a couple of hold-outs. Move those to the
    ES6 Map class as well.

 src/joinDialog.js    | 16 ++++++++--------
 src/tabCompletion.js | 12 ++++++------
 src/userList.js      | 12 ++++++------
 3 files changed, 20 insertions(+), 20 deletions(-)
---
diff --git a/src/joinDialog.js b/src/joinDialog.js
index 89abe94..856a057 100644
--- a/src/joinDialog.js
+++ b/src/joinDialog.js
@@ -55,18 +55,18 @@ var JoinDialog = GObject.registerClass({
 
         this._accountsMonitor = AccountsMonitor.getDefault();
 
-        this._accounts = {};
+        this._accounts = new Map();
         this._accountsMonitor.enabledAccounts.forEach(a => {
-            this._accounts[a.display_name] = a;
+            this._accounts.set(a.display_name, a);
         });
         this._accountAddedId =
             this._accountsMonitor.connect('account-added', (am, account) => {
-                this._accounts[account.display_name] = account;
+                this._accounts.set(account.display_name, account);
                 this._updateConnectionCombo();
             });
         this._accountRemovedId =
             this._accountsMonitor.connect('account-removed', (am, account) => {
-                delete this._accounts[account.display_name];
+                this._accounts.delete(account.display_name);
                 this._updateConnectionCombo();
             });
 
@@ -90,7 +90,7 @@ var JoinDialog = GObject.registerClass({
     }
 
     get _hasAccounts() {
-      return Object.keys(this._accounts).length > 0;
+      return this._accounts.size > 0;
     }
 
     _setupMainPage() {
@@ -150,7 +150,7 @@ var JoinDialog = GObject.registerClass({
 
     _onAccountChanged() {
         let selected = this._connectionCombo.get_active_text();
-        let account = this._accounts[selected];
+        let account = this._accounts.get(selected);
         if (!account)
             return;
 
@@ -165,7 +165,7 @@ var JoinDialog = GObject.registerClass({
         this.hide();
 
         let selected = this._connectionCombo.get_active_text();
-        let account = this._accounts[selected];
+        let account = this._accounts.get(selected);
 
         let toJoinRooms = this._serverRoomList.selectedRooms;
         toJoinRooms.forEach(room => {
@@ -184,7 +184,7 @@ var JoinDialog = GObject.registerClass({
     _updateConnectionCombo() {
         this._connectionCombo.remove_all();
 
-        let names = Object.keys(this._accounts).sort((a, b) => {
+        let names = [...this._accounts.keys()].sort((a, b) => {
             // TODO: figure out combo box sorting
             return (a < b) ? -1 : ((a > b) ? 1 : 0);
         });
diff --git a/src/tabCompletion.js b/src/tabCompletion.js
index addf108..eb7fc6c 100644
--- a/src/tabCompletion.js
+++ b/src/tabCompletion.js
@@ -34,7 +34,7 @@ var TabCompletion = class {
         this._list.connect('keynav-failed', Lang.bind(this, this._onKeynavFailed));
         frame.add(this._list);
 
-        this._widgetMap = {};
+        this._widgetMap = new Map();
         this._previousWasCommand = false;
 
         let commands = Object.keys(IrcParser.knownCommands);
@@ -82,14 +82,14 @@ var TabCompletion = class {
             return;
         }
 
-        let widgetMap = {};
+        let widgetMap = new Map();
 
         for (let i = 0; i < completions.length; i++) {
             let nick = completions[i];
-            let row = this._widgetMap[nick];
+            let row = this._widgetMap.get(nick);
 
             if (row) {
-                widgetMap[nick] = row;
+                widgetMap.set(nick, row);
                 this._list.remove(row);
             } else {
                 row = new Gtk.ListBoxRow();
@@ -99,7 +99,7 @@ var TabCompletion = class {
                                         halign: Gtk.Align.START,
                                         margin_start: 6,
                                         margin_end: 6 }));
-                widgetMap[nick] = row;
+                widgetMap.set(nick, row);
             }
         }
 
@@ -112,7 +112,7 @@ var TabCompletion = class {
         });
 
         for (let i = 0; i < completions.length; i++) {
-            let row = this._widgetMap[completions[i]];
+            let row = this._widgetMap.get(completions[i]);
             this._list.add(row);
         }
         this._canComplete = completions.length > 0;
diff --git a/src/userList.js b/src/userList.js
index 3dbf207..8364048 100644
--- a/src/userList.js
+++ b/src/userList.js
@@ -584,7 +584,7 @@ class UserList extends Gtk.ScrolledWindow {
                      Lang.bind(this, this._onDestroy));
 
         this._room = room;
-        this._rows = {};
+        this._rows = new Map();
         this._activeRow = null;
 
         let roomSignals = [
@@ -618,7 +618,7 @@ class UserList extends Gtk.ScrolledWindow {
     }
 
     get numRows() {
-        return Object.keys(this._rows).length;
+        return this.rows.size;
     }
 
     _onDestroy() {
@@ -675,7 +675,7 @@ class UserList extends Gtk.ScrolledWindow {
 
     _onChannelChanged(room) {
         this._list.foreach(w => { w.destroy(); });
-        this._rows = {};
+        this._rows.clear();
 
         if (!room.channel)
             return;
@@ -687,15 +687,15 @@ class UserList extends Gtk.ScrolledWindow {
 
     _addMember(member) {
         let row = new UserListRow(member);
-        this._rows[member] = row;
+        this._rows.set(member, row);
         this._list.add(row);
     }
 
     _removeMember(member) {
-        let row = this._rows[member];
+        let row = this._rows.get(member);
         if (row)
             row.destroy();
-        delete this._rows[member];
+        this._rows.delete(member);
     }
 
     _setActiveRow(row) {


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