[polari/wip/fmuellner/cleanups: 3/4] cleanup: Use class methods for singleton pattern



commit 0c2cece350c1d83d815a80fb787916fee1cbc4b5
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   | 16 ++++++++--------
 src/networksManager.js   | 16 ++++++++--------
 src/roomManager.js       | 16 ++++++++--------
 src/serverRoomManager.js | 16 ++++++++--------
 src/userTracker.js       | 15 +++++++--------
 5 files changed, 39 insertions(+), 40 deletions(-)
---
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index a2fb92d..52755a0 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -4,15 +4,13 @@ const Tp = imports.gi.TelepathyGLib;
 const Lang = imports.lang;
 const Signals = imports.signals;
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new AccountsMonitor();
-    return _singleton;
-}
-
 class AccountsMonitor {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new AccountsMonitor();
+        return this._singleton;
+    }
+
     constructor() {
         this._accounts = new Map();
         this._accountSettings = new Map();
@@ -151,3 +149,5 @@ class AccountsMonitor {
     }
 };
 Signals.addSignalMethods(AccountsMonitor.prototype);
+
+var getDefault = AccountsMonitor.getDefault;
diff --git a/src/networksManager.js b/src/networksManager.js
index efd18b4..206fcc4 100644
--- a/src/networksManager.js
+++ b/src/networksManager.js
@@ -3,15 +3,13 @@ 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();
@@ -120,3 +118,5 @@ var NetworksManager = class {
     }
 };
 Signals.addSignalMethods(NetworksManager.prototype);
+
+var getDefault = NetworksManager.getDefault;
diff --git a/src/roomManager.js b/src/roomManager.js
index 4af710e..dca9812 100644
--- a/src/roomManager.js
+++ b/src/roomManager.js
@@ -7,15 +7,13 @@ const AccountsMonitor = imports.accountsMonitor;
 const Lang = imports.lang;
 const Signals = imports.signals;
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new _RoomManager();
-    return _singleton;
-}
-
 class _RoomManager {
+    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' })
@@ -202,3 +200,5 @@ class _RoomManager {
     }
 };
 Signals.addSignalMethods(_RoomManager.prototype);
+
+var getDefault = _RoomManager.getDefault;
diff --git a/src/serverRoomManager.js b/src/serverRoomManager.js
index 3bc60a3..28affa2 100644
--- a/src/serverRoomManager.js
+++ b/src/serverRoomManager.js
@@ -15,15 +15,13 @@ 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;
-}
-
 class _ServerRoomManager {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new _ServerRoomManager();
+        return this._singleton;
+    }
+
     constructor() {
         this._roomLists = new Map();
 
@@ -372,3 +370,5 @@ var ServerRoomList = GObject.registerClass({
         this.notify('can-join');
     }
 });
+
+var getDefault = _ServerRoomManager.getDefault;
diff --git a/src/userTracker.js b/src/userTracker.js
index b0506a6..90ff3f0 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -10,15 +10,13 @@ const GLib = imports.gi.GLib;
 const AccountsMonitor = imports.accountsMonitor;
 const RoomManager = imports.roomManager;
 
-let _singleton = null;
-
-function getUserStatusMonitor() {
-    if (_singleton == null)
-        _singleton = new UserStatusMonitor();
-    return _singleton;
-}
-
 class UserStatusMonitor {
+    static getDefault() {
+        if (!this._singleton)
+            this._singleton = new UserStatusMonitor();
+        return this._singleton;
+    }
+
     constructor() {
         this._userTrackers = new Map();
         this._accountsMonitor = AccountsMonitor.getDefault();
@@ -48,6 +46,7 @@ class UserStatusMonitor {
     }
 };
 
+var getUserStatusMonitor = UserStatusMonitor.getDefault;
 
 var UserTracker = GObject.registerClass({
     Signals: {


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