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



commit fffec0eb515ab3c7bcde2814e97cb7f50bdfe03c
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 d5d5e46..2e62ecc 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 92f0005..04f3515 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -126,11 +126,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);
     },
 
@@ -178,4 +175,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 23005fa..b92d58c 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, 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) {
@@ -86,8 +95,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]