[gnome-maps/wip/fix-geoloc: 8/11] Utils: add new method 'once'



commit b4fef18ebf6f082f659c58d082953b93d10c9655
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Wed Jul 24 07:21:37 2013 +0200

    Utils: add new method 'once'
    
    Utils.once connects to a signal once and then disconnects on first
    emission.
    Since I don't know if it is possible to monkey patch GObject prototypes
    this it is only added to the prototypes of js objects. GObjects need
    to call Utils.once(gobject, signalName, callback);
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704537

 src/application.js |    2 +-
 src/geoclue.js     |    2 +-
 src/mapLocation.js |    9 +++------
 src/mapView.js     |   12 ++++++------
 src/utils.js       |   13 +++++++++++++
 5 files changed, 24 insertions(+), 14 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 8df1718..bd456bf 100644
--- a/src/application.js
+++ b/src/application.js
@@ -109,4 +109,4 @@ const Application = new Lang.Class({
         this._mainWindow = null;
     }
 });
-Signals.addSignalMethods(Application.prototype);
+Utils.addSignalMethods(Application.prototype);
diff --git a/src/geoclue.js b/src/geoclue.js
index 033f0f9..7e38f09 100644
--- a/src/geoclue.js
+++ b/src/geoclue.js
@@ -134,4 +134,4 @@ const Geoclue = new Lang.Class({
         }
     }
 });
-Signals.addSignalMethods(Geoclue.prototype);
+Utils.addSignalMethods(Geoclue.prototype);
diff --git a/src/mapLocation.js b/src/mapLocation.js
index 92f4180..3c731fa 100644
--- a/src/mapLocation.js
+++ b/src/mapLocation.js
@@ -67,11 +67,8 @@ const MapLocation = new Lang.Class({
          * location.
          */
 
-        let id = this._view.connect("animation-completed", (function() {
-            this._view.disconnect(id);
-
-            id = this._view.connect("animation-completed::go-to", (function() {
-                this._view.disconnect(id);
+        Utils.once(this._view, "animation-completed", (function() {
+            Utils.once(this._view, "animation-completed::go-to", (function() {
                 this.zoomToFit();
                 this.emit('gone-to');
             }).bind(this));
@@ -132,4 +129,4 @@ const MapLocation = new Lang.Class({
         });
     }
 });
-Signals.addSignalMethods(MapLocation.prototype);
+Utils.addSignalMethods(MapLocation.prototype);
diff --git a/src/mapView.js b/src/mapView.js
index 5bafdba..c5dc949 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -123,11 +123,11 @@ const MapView = new Lang.Class({
     },
 
     gotoUserLocation: function(animate) {
-        let goneToId = this._userLocation.connect("gone-to", Lang.bind(this,
-            function() {
-                this.emit('gone-to-user-location');
-                this._userLocation.disconnect(goneToId);
-            }));
+        this.emit('going-to-user-location');
+        this._userLocation.once("gone-to", (function() {
+            this.emit('gone-to-user-location');
+        }).bind(this));
+
         this._userLocation.goTo(animate);
     },
 
@@ -171,4 +171,4 @@ const MapView = new Lang.Class({
         this.emit('view-moved');
     }
 });
-Signals.addSignalMethods(MapView.prototype);
+Utils.addSignalMethods(MapView.prototype);
diff --git a/src/utils.js b/src/utils.js
index 60c4d3f..621d9ef 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -50,6 +50,19 @@ function debug(str) {
         log('DEBUG: ' + str);
 }
 
+// Connect to a signal on an object and disconnect on its first emission.
+function once(obj, signal, callback) {
+    let id = obj.connect(signal, function() {
+        obj.disconnect(id);
+        callback();
+    });
+}
+
+function addSignalMethods(proto) {
+    Signals.addSignalMethods(proto);
+    proto.once = once.bind(undefined, proto);
+}
+
 function loadStyleSheet(file) {
     let provider = new Gtk.CssProvider();
     provider.load_from_file(file);


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