[gnome-maps] Utils: add new method 'once'



commit 5af6b9b4023e2a19700ec1826d35b346af249a6f
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.
    The method can also be added to the prototype of select objects with
    Utils.addSignalMethods, much like Signals.addSignalMethods
    Since I don't know how to monkey patch GObject prototypes this can
    only be added to the prototypes of pure JavaScript 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     |    4 ++--
 src/utils.js       |   13 +++++++++++++
 5 files changed, 20 insertions(+), 10 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 65867a3..17be539 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 5319eb1..e2e5cfa 100644
--- a/src/geoclue.js
+++ b/src/geoclue.js
@@ -133,4 +133,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 f5d6395..60c8619 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -123,9 +123,9 @@ const MapView = new Lang.Class({
     },
 
     gotoUserLocation: function(animate) {
-        let goneToId = this._userLocation.connect("gone-to", (function() {
+        this.emit('going-to-user-location');
+        this._userLocation.once("gone-to", (function() {
             this.emit('gone-to-user-location');
-            this._userLocation.disconnect(goneToId);
         }).bind(this));
         this._userLocation.goTo(animate);
     },
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]