[polari/wip/fmuellner/nickserv: 49/62] telepathyClient: Stop using the singleton pattern



commit 3b0eae3fcc0396e57935c9b4cb45ed5247cd07c4
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Jul 30 21:58:38 2016 +0200

    telepathyClient: Stop using the singleton pattern
    
    The pattern made a lot of sense while many components needed to access it
    for its room management, but now that it has been reduced to its function
    as telepathy client, it is cleaner to make it a regular class that is
    instantiated by the application when its functionality is actually
    needed.
    This also allows us to implement Telepathy.BaseClient directly instead of
    using a helper class, and to get rid of the awkward lateInit() split.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769655

 src/application.js     |   14 +++++++--
 src/telepathyClient.js |   70 ++++++++++-------------------------------------
 2 files changed, 26 insertions(+), 58 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 2e7974b..b86c44b 100644
--- a/src/application.js
+++ b/src/application.js
@@ -118,8 +118,9 @@ const Application = new Lang.Class({
         for (let i = 1; i < 10; i++)
             this.set_accels_for_action('app.nth-room(%d)'.format(i), ['<Alt>' + i]);
 
+        this._telepathyClient = null;
+
         this._roomManager = RoomManager.getDefault();
-        this._client = TelepathyClient.getDefault();
         this._accountsMonitor = AccountsMonitor.getDefault();
         this._networksManager = NetworksManager.getDefault();
 
@@ -132,13 +133,20 @@ const Application = new Lang.Class({
     },
 
     vfunc_activate: function() {
+        if (!this._telepathyClient) {
+            let params = {
+                name: 'Polari',
+                account_manager: this._accountsMonitor.accountManager,
+                uniquify_name: false
+            };
+            this._telepathyClient = new TelepathyClient.TelepathyClient(params);
+        }
+
         if (!this._window) {
             this._window = new MainWindow.MainWindow({ application: this });
             this._window.connect('destroy',
                                  () => { this.emit('prepare-shutdown'); });
             this._window.show_all();
-
-            this._client.lateInit();
         }
         this._window.present();
     },
diff --git a/src/telepathyClient.js b/src/telepathyClient.js
index 02242a6..13d49ce 100644
--- a/src/telepathyClient.js
+++ b/src/telepathyClient.js
@@ -29,14 +29,6 @@ const SASLAuthenticationIface = '<node> \
 </node>';
 let SASLAuthProxy = Gio.DBusProxy.makeProxyWrapper(SASLAuthenticationIface);
 
-let _singleton = null;
-
-function getDefault() {
-    if (_singleton == null)
-        _singleton = new _TelepathyClient();
-    return _singleton;
-}
-
 const SASLStatus = {
     NOT_STARTED: 0,
     IN_PROGRESS: 1,
@@ -118,34 +110,11 @@ const SASLAuthHandler = new Lang.Class({
     }
 });
 
-const Client = new Lang.Class({
-    Name: 'Client',
-    GTypeName: 'PolariTpClient',
+const TelepathyClient = new Lang.Class({
+    Name: 'TelepathyClient',
     Extends: Tp.BaseClient,
 
-    _init: function(am, manager) {
-        this.parent({ account_manager: am,
-                      name: 'Polari',
-                      uniquify_name: false });
-        this.set_handler_bypass_approval(false);
-        this.set_observer_recover(true);
-
-        this._manager = manager;
-    },
-
-    vfunc_observe_channels: function() {
-        this._manager.observeChannels.apply(this._manager, arguments);
-    },
-
-    vfunc_handle_channels: function() {
-        this._manager.handleChannels.apply(this._manager, arguments);
-    }
-});
-
-const _TelepathyClient = new Lang.Class({
-    Name: '_TelepathyClient',
-
-    _init: function() {
+    _init: function(params) {
         this._app = Gio.Application.get_default();
         this._app.connect('prepare-shutdown', () => {
             [...this._pendingRequests.values()].forEach(r => { r.cancel(); });
@@ -153,13 +122,17 @@ const _TelepathyClient = new Lang.Class({
 
         this._pendingRequests = new Map();
 
+        this.parent(params);
+
+        this.set_handler_bypass_approval(false);
+        this.set_observer_recover(true);
+
         this._networkMonitor = Gio.NetworkMonitor.get_default();
         this._roomManager = RoomManager.getDefault();
         this._roomManager.connect('room-added', (mgr, room) => {
             this._connectRoom(room);
         });
         this._accountsMonitor = AccountsMonitor.getDefault();
-        this._amIsPrepared = false;
         this._accountsMonitor.prepare(Lang.bind(this, this._onPrepared));
     },
 
@@ -180,8 +153,6 @@ const _TelepathyClient = new Lang.Class({
             this._app.lookup_action(a.name).connect('activate', a.handler);
         });
 
-        this._client = new Client(this._accountsMonitor.accountManager, this);
-
         let filters = [];
 
         let roomFilter = {};
@@ -199,22 +170,11 @@ const _TelepathyClient = new Lang.Class({
         authFilter[Tp.PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD] = 
Tp.IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION;
         filters.push(authFilter);
 
-        filters.forEach(Lang.bind(this,
-            function(f) {
-                this._client.add_handler_filter(f);
-                this._client.add_observer_filter(f);
-            }));
-        this._client.register();
-
-        this._amIsPrepared = true;
-        this.lateInit();
-    },
-
-    lateInit: function() {
-        let ready = this._amIsPrepared &&
-            this._app.get_active_window() != null;
-        if (!ready)
-            return;
+        filters.forEach(f => {
+            this.add_handler_filter(f);
+            this.add_observer_filter(f);
+        });
+        this.register();
 
         this._accountsMonitor.connect('account-enabled',
                                       Lang.bind(this, this._onAccountEnabled));
@@ -387,7 +347,7 @@ const _TelepathyClient = new Lang.Class({
         context.accept();
     },
 
-    observeChannels: function() {
+    vfunc_observe_channels: function() {
         let [account, connection,
              channels, op, requests, context] = arguments;
 
@@ -408,7 +368,7 @@ const _TelepathyClient = new Lang.Class({
             }));
     },
 
-    handleChannels: function() {
+    vfunc_handle_channels: function() {
         let [account, connection,
              channels, satisfied, userTime, context] = arguments;
 


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