[gjs/wip/gcampax/65-gobject-signal] Allow using GObject.signal_* functions in place of Object methods



commit 7547841175c45da9e0628f725243442bf82e0f29
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Fri Apr 19 18:51:33 2013 +0200

    Allow using GObject.signal_* functions in place of Object methods
    
    GObject.Object.connect() and friends might be unavailable because
    shadowed by another method in the prototype chain (such as
    g_socket_connect()).
    In that case, it is nice to use the C equivalents, which are
    GObject.signal_connect(), GObject.signal_handler_disconnect() and
    GObject.signal_emit_by_name().
    
    (Philip Chimento: Rebased, ported test to Jasmine, and fixed coding
    style.)
    
    Closes #65.

 installed-tests/js/testEverythingBasic.js | 24 ++++++++++++++++++++++++
 modules/overrides/GObject.js              | 14 ++++++++++++++
 2 files changed, 38 insertions(+)
---
diff --git a/installed-tests/js/testEverythingBasic.js b/installed-tests/js/testEverythingBasic.js
index 98e4f45..1779388 100644
--- a/installed-tests/js/testEverythingBasic.js
+++ b/installed-tests/js/testEverythingBasic.js
@@ -480,6 +480,30 @@ describe('Life, the Universe and Everything', function () {
         }).pend('Not yet implemented');
     });
 
+    describe('Signal alternative syntax', function () {
+        let o, handler;
+        beforeEach(function () {
+            handler = jasmine.createSpy('handler');
+            o = new Regress.TestObj();
+            let handlerId = GObject.signal_connect(o, 'test', handler);
+            handler.and.callFake(() =>
+                GObject.signal_handler_disconnect(o, handlerId));
+
+            GObject.signal_emit_by_name(o, 'test');
+        });
+
+        it('handler is called with the right object', function () {
+            expect(handler).toHaveBeenCalledTimes(1);
+            expect(handler).toHaveBeenCalledWith(o);
+        });
+
+        it('disconnected handler is not called', function () {
+            handler.calls.reset();
+            GObject.signal_emit_by_name(o, 'test');
+            expect(handler).not.toHaveBeenCalled();
+        });
+    });
+
     it('torture signature 0', function () {
         let [y, z, q] = Regress.test_torture_signature_0(42, 'foo', 7);
         expect(Math.floor(y)).toEqual(42);
diff --git a/modules/overrides/GObject.js b/modules/overrides/GObject.js
index abcbd23..0050afe 100644
--- a/modules/overrides/GObject.js
+++ b/modules/overrides/GObject.js
@@ -451,4 +451,18 @@ function _init() {
     this.Object.prototype.disconnect = function(id) {
         return GObject.signal_handler_disconnect(this, id);
     };
+
+    // A simple workaround if you have a class with .connect, .disconnect or .emit
+    // methods (such as Gio.Socket.connect or NMClient.Device.disconnect)
+    // The original g_signal_* functions are not introspectable anyway, because
+    // we need our own handling of signal argument marshalling
+    this.signal_connect = function(object, name, handler) {
+        return GObject.Object.prototype.connect.call(object, name, handler);
+    };
+    this.signal_connect_after = function(object, name, handler) {
+        return GObject.Object.prototype.connect_after.call(object, name, handler);
+    };
+    this.signal_emit_by_name = function(object, ...nameAndArgs) {
+        return GObject.Object.prototype.emit.apply(object, nameAndArgs);
+    };
 }


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