[gjs] signals: Add WithSignals interface



commit 66d531a7af9b6613acb706328ea87c6d4ec6bd17
Author: Philip Chimento <philip chimento gmail com>
Date:   Thu Jan 5 20:58:53 2017 -0800

    signals: Add WithSignals interface
    
    This is an alternative to using Signals.addSignalMethods(). A class can
    implement Signals.WithSignals to get the signal methods built in.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=664897

 installed-tests/js/testSignals.js |   20 +++++++++++++++++---
 modules/signals.js                |   10 ++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/installed-tests/js/testSignals.js b/installed-tests/js/testSignals.js
index 1ea4d27..5944b29 100644
--- a/installed-tests/js/testSignals.js
+++ b/installed-tests/js/testSignals.js
@@ -4,14 +4,28 @@ const Signals = imports.signals;
 
 const Foo = new Lang.Class({
     Name: 'Foo',
+    Implements: [Signals.WithSignals],
     _init: function () {},
 });
-Signals.addSignalMethods(Foo.prototype);
 
 describe('Object with signals', function () {
+    testSignals(Foo);
+});
+
+const FooWithoutSignals = new Lang.Class({
+    Name: 'FooWithoutSignals',
+    _init: function () {},
+});
+Signals.addSignalMethods(FooWithoutSignals.prototype);
+
+describe('Object with signals added', function () {
+    testSignals(FooWithoutSignals);
+});
+
+function testSignals(klass) {
     let foo, bar;
     beforeEach(function () {
-        foo = new Foo();
+        foo = new klass();
         bar = jasmine.createSpy('bar');
     });
 
@@ -106,4 +120,4 @@ describe('Object with signals', function () {
             expect(bar2).toHaveBeenCalledTimes(2);
         });
     });
-});
+}
diff --git a/modules/signals.js b/modules/signals.js
index 2b0d03a..8db97c2 100644
--- a/modules/signals.js
+++ b/modules/signals.js
@@ -26,6 +26,8 @@
 // 3) the expectation is that a given object will have a very small number of
 //    connections, but they may be to different signal names
 
+const Lang = imports.lang;
+
 function _connect(name, callback) {
     // be paranoid about callback arg since we'd start to throw from emit()
     // if it was messed up
@@ -153,3 +155,11 @@ function addSignalMethods(proto) {
     // this one is not in GObject, but useful
     _addSignalMethod(proto, "disconnectAll", _disconnectAll);
 }
+
+const WithSignals = new Lang.Interface({
+    Name: 'WithSignals',
+    connect: _connect,
+    disconnect: _disconnect,
+    emit: _emit,
+    disconnectAll: _disconnectAll,
+});


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