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



commit b309849c34c44c3b7e2bf7089d7bf3ee9dfc69fa
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Wed Jul 17 06:51:18 2013 +0200

    Utils: add new method 'once'
    
    Utils.once connects to a signal once and then immediately disconnects.
    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);

 src/application.js |    2 +-
 src/geoclue.js     |    2 +-
 src/mapLocation.js |    9 +++------
 src/mapView.js     |    9 +++------
 src/utils.js       |   21 ++++++++++++++-------
 5 files changed, 22 insertions(+), 21 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 10ec1dc..bd456bf 100644
--- a/src/application.js
+++ b/src/application.js
@@ -109,4 +109,4 @@ const Application = new Lang.Class({
         this._mainWindow = null;
     }
 });
-Utils.addJSSignalMethods(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 b60a646..361614e 100644
--- a/src/mapLocation.js
+++ b/src/mapLocation.js
@@ -71,11 +71,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));
@@ -125,4 +122,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 730898c..adaff7a 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -128,11 +128,8 @@ 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._userLocation.once("gone-to",
+                                this.emit.bind(this, 'gone-to-user-location'));
         this._userLocation.goTo(animate);
     },
 
@@ -180,4 +177,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 0699daf..3d52fbe 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -50,11 +50,20 @@ function debug(str) {
         log('DEBUG: ' + str);
 }
 
-function addJSSignalMethods(proto) {
-    proto.connectJS = Signals._connect;
-    proto.disconnectJS = Signals._disconnect;
-    proto.emitJS = Signals._emit;
-    proto.disconnectAllJS = Signals._disconnectAll;
+/**
+ * Connect to a signal on an object and immediately
+ * disconnect on first call.
+ */
+function once(obj, signal, f) {
+    let id = obj.connect(signal, function() {
+        obj.disconnect(id);
+        f && f();
+    });
+}
+
+function addSignalMethods(proto) {
+    Signals.addSignalMethods(proto);
+    proto.once = once.bind(undefined, proto);
 }
 
 function loadStyleSheet(file) {
@@ -89,8 +98,6 @@ function initActions(actionMap, simpleActionEntries, context) {
     });
 }
 
-
-
 function CreateActorFromImageFile(path) {
     try {
         let pixbuf = GdkPixbuf.Pixbuf.new_from_file(path);


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