[polari/wip/fmuellner/cleanups: 10/11] cleanup: Use class methods for singleton pattern



commit ad43c206e87299093fe5a4aa832d0424a40e825e
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Nov 5 01:22:07 2017 +0100

    cleanup: Use class methods for singleton pattern
    
    ES6 classes support class methods, so use that for a more idiomatic
    singleton pattern ...

 src/accountsMonitor.js   | 14 ++++++--------
 src/application.js       | 12 ++++++------
 src/chatView.js          |  4 ++--
 src/connections.js       |  4 ++--
 src/ircParser.js         |  2 +-
 src/joinDialog.js        |  2 +-
 src/mainWindow.js        |  4 ++--
 src/networksManager.js   | 14 ++++++--------
 src/roomList.js          |  4 ++--
 src/roomManager.js       | 16 +++++++---------
 src/roomStack.js         |  4 ++--
 src/serverRoomManager.js | 20 +++++++++-----------
 src/telepathyClient.js   |  4 ++--
 src/userTracker.js       | 18 ++++++++----------
 14 files changed, 56 insertions(+), 66 deletions(-)
---
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index 4171f00..c303f15 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -3,15 +3,13 @@ const Lang = imports.lang;
 const Signals = imports.signals;
 const Tp = imports.gi.TelepathyGLib;
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new AccountsMonitor();
-    return _singleton;
-}
-
 var AccountsMonitor = class {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new AccountsMonitor();
+        return this._singleton;
+    }
+
     constructor() {
         this._accounts = new Map();
         this._accountSettings = new Map();
diff --git a/src/application.js b/src/application.js
index f91e5bd..090766a 100644
--- a/src/application.js
+++ b/src/application.js
@@ -7,17 +7,17 @@ const Lang = imports.lang;
 const Polari = imports.gi.Polari;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
+const {AccountsMonitor} = imports.accountsMonitor;
 const AppNotifications = imports.appNotifications;
 const Connections = imports.connections;
 const {InitialSetupWindow} = imports.initialSetup;
 const {MainWindow} = imports.mainWindow;
-const NetworksManager = imports.networksManager;
+const {NetworksManager} = imports.networksManager;
 const {PasteManager} = imports.pasteManager;
-const RoomManager = imports.roomManager;
-const ServerRoomManager = imports.serverRoomManager;
+const {RoomManager} = imports.roomManager;
+const {ServerRoomManager} = imports.serverRoomManager;
 const {TelepathyClient} = imports.telepathyClient;
-const UserTracker = imports.userTracker;
+const {UserStatusMonitor} = imports.userTracker;
 const Utils = imports.utils;
 
 const MAX_RETRIES = 3;
@@ -249,7 +249,7 @@ var Application = GObject.registerClass({
 
         this._roomManager = RoomManager.getDefault();
         this._accountsMonitor = AccountsMonitor.getDefault();
-        this._userStatusMonitor = UserTracker.getUserStatusMonitor();
+        this._userStatusMonitor = UserStatusMonitor.getDefault();
         this._networksManager = NetworksManager.getDefault();
         this._serverRoomManager = ServerRoomManager.getDefault();
 
diff --git a/src/chatView.js b/src/chatView.js
index 4a1956a..5c43968 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -13,7 +13,7 @@ const Tpl = imports.gi.TelepathyLogger;
 
 const {DropTargetIface} = imports.pasteManager;
 const {UserPopover} = imports.userList;
-const UserTracker = imports.userTracker;
+const {UserStatusMonitor} = imports.userTracker;
 const Utils = imports.utils;
 
 var MAX_NICK_CHARS = 8;
@@ -353,7 +353,7 @@ var ChatView = GObject.registerClass({
         this._backlogTimeoutId = 0;
         this._statusCount = { left: 0, joined: 0, total: 0 };
 
-        let statusMonitor = UserTracker.getUserStatusMonitor();
+        let statusMonitor = UserStatusMonitor.getDefault();
         this._userTracker = statusMonitor.getUserTrackerForAccount(room.account);
 
         this._room.account.connect('notify::nickname', () => {
diff --git a/src/connections.js b/src/connections.js
index 0989f36..39b64d9 100644
--- a/src/connections.js
+++ b/src/connections.js
@@ -4,8 +4,8 @@ const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-const NetworksManager = imports.networksManager;
+const {AccountsMonitor} = imports.accountsMonitor;
+const {NetworksManager} = imports.networksManager;
 const Utils = imports.utils;
 
 const DEFAULT_PORT = 6667;
diff --git a/src/ircParser.js b/src/ircParser.js
index d2cb524..1c19a41 100644
--- a/src/ircParser.js
+++ b/src/ircParser.js
@@ -4,7 +4,7 @@ const Signals = imports.signals;
 const Tp = imports.gi.TelepathyGLib;
 
 const AppNotifications = imports.appNotifications;
-const RoomManager = imports.roomManager;
+const {RoomManager} = imports.roomManager;
 const Utils = imports.utils;
 
 const N_ = s => s;
diff --git a/src/joinDialog.js b/src/joinDialog.js
index 35aa0cd..c1bd19c 100644
--- a/src/joinDialog.js
+++ b/src/joinDialog.js
@@ -5,7 +5,7 @@ const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 
-const AccountsMonitor = imports.accountsMonitor;
+const {AccountsMonitor} = imports.accountsMonitor;
 const Utils = imports.utils;
 
 const DialogPage = {
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 8ca80be..2958937 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -8,10 +8,10 @@ const Mainloop = imports.mainloop;
 const Polari = imports.gi.Polari;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
+const {AccountsMonitor} = imports.accountsMonitor;
 const {JoinDialog} = imports.joinDialog;
 const RoomList = imports.roomList; // used in template
-const RoomManager = imports.roomManager;
+const {RoomManager} = imports.roomManager;
 const RoomStack = imports.roomStack; // used in template
 const UserList = imports.userList; // used in template
 const Utils = imports.utils;
diff --git a/src/networksManager.js b/src/networksManager.js
index 209d59f..7532a70 100644
--- a/src/networksManager.js
+++ b/src/networksManager.js
@@ -2,15 +2,13 @@ const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Signals = imports.signals;
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new NetworksManager();
-    return _singleton;
-}
-
 var NetworksManager = class {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new NetworksManager();
+        return this._singleton;
+    }
+
     constructor() {
         this._networks = [];
         this._networksById = new Map();
diff --git a/src/roomList.js b/src/roomList.js
index 677457c..796fb54 100644
--- a/src/roomList.js
+++ b/src/roomList.js
@@ -6,8 +6,8 @@ const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-const RoomManager = imports.roomManager;
+const {AccountsMonitor} = imports.accountsMonitor;
+const {RoomManager} = imports.roomManager;
 
 function _onPopoverVisibleChanged(popover) {
     let context = popover.relative_to.get_style_context();
diff --git a/src/roomManager.js b/src/roomManager.js
index 45e9aa8..392e18b 100644
--- a/src/roomManager.js
+++ b/src/roomManager.js
@@ -5,17 +5,15 @@ const Polari = imports.gi.Polari;
 const Signals = imports.signals;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new RoomManager();
-    return _singleton;
-}
+const {AccountsMonitor} = imports.accountsMonitor;
 
 var RoomManager = class {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new RoomManager();
+        return this._singleton;
+    }
+
     constructor() {
         this._rooms = new Map();
         this._settings = new Gio.Settings({ schema_id: 'org.gnome.Polari' })
diff --git a/src/roomStack.js b/src/roomStack.js
index af3e9c1..4ef81d1 100644
--- a/src/roomStack.js
+++ b/src/roomStack.js
@@ -6,10 +6,10 @@ const Lang = imports.lang;
 const Pango = imports.gi.Pango;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
+const {AccountsMonitor} = imports.accountsMonitor;
 const {ChatView} = imports.chatView;
 const {EntryArea} = imports.entryArea;
-const RoomManager = imports.roomManager;
+const {RoomManager} = imports.roomManager;
 
 var RoomStack = GObject.registerClass({
     Properties: {
diff --git a/src/serverRoomManager.js b/src/serverRoomManager.js
index 450ee6d..36cb93a 100644
--- a/src/serverRoomManager.js
+++ b/src/serverRoomManager.js
@@ -7,22 +7,20 @@ const Mainloop = imports.mainloop;
 const Signals = imports.signals;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-const RoomManager = imports.roomManager;
+const {AccountsMonitor} = imports.accountsMonitor;
+const {RoomManager} = imports.roomManager;
 const Utils = imports.utils;
 
 const MS_PER_IDLE = 10; // max time spend in idle
 const MS_PER_FILTER_IDLE = 5; // max time spend in idle while filtering
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new ServerRoomManager();
-    return _singleton;
-}
-
 var ServerRoomManager = class {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new ServerRoomManager();
+        return this._singleton;
+    }
+
     constructor() {
         this._roomLists = new Map();
 
@@ -189,7 +187,7 @@ var ServerRoomList = GObject.registerClass({
                 this._toggleChecked(Gtk.TreePath.new_from_string(pathStr));
         });
 
-        this._manager = getDefault();
+        this._manager = ServerRoomManager.getDefault();
         this._manager.connect('loading-changed',
                               Lang.bind(this, this._onLoadingChanged));
     }
diff --git a/src/telepathyClient.js b/src/telepathyClient.js
index acc9d36..b7c17f3 100644
--- a/src/telepathyClient.js
+++ b/src/telepathyClient.js
@@ -5,8 +5,8 @@ const Lang = imports.lang;
 const Polari = imports.gi.Polari;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-const RoomManager = imports.roomManager;
+const {AccountsMonitor} = imports.accountsMonitor;
+const {RoomManager} = imports.roomManager;
 const Utils = imports.utils;
 
 const SASLAuthenticationIface = '<node> \
diff --git a/src/userTracker.js b/src/userTracker.js
index 143fd5a..ebd8fd1 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -5,19 +5,17 @@ const Lang = imports.lang;
 const Polari = imports.gi.Polari;
 const Tp = imports.gi.TelepathyGLib;
 
-const AccountsMonitor = imports.accountsMonitor;
-const RoomManager = imports.roomManager;
+const {AccountsMonitor} = imports.accountsMonitor;
+const {RoomManager} = imports.roomManager;
 const Utils = imports.utils;
 
-let _singleton = null;
-
-function getUserStatusMonitor() {
-    if (_singleton == null)
-        _singleton = new UserStatusMonitor();
-    return _singleton;
-}
-
 var UserStatusMonitor = class {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new UserStatusMonitor();
+        return this._singleton;
+    }
+
     constructor() {
         this._userTrackers = new Map();
         this._accountsMonitor = AccountsMonitor.getDefault();


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