Permalink
Browse files

Added Signal.init and tests for passing args to callbacks.

  • Loading branch information...
1 parent ee87dfb commit 653992a655d7bbf4f6076121880e651da7850c11 @PhilHarnish committed Jan 3, 2012
Showing with 35 additions and 5 deletions.
  1. +28 −4 spec/always/SignalTest.js
  2. +7 −1 src/always/Signal.js
View
32 spec/always/SignalTest.js
@@ -1,20 +1,36 @@
var spec = require("../../test"),
Signal = require("always/Signal.js");
-describe("Signal", function () {
+describe("Mixin", function () {
+ it("should mixin specified signals.", function () {
+ var o = {
+ signals: {
+ signalA: true,
+ signalB: true
+ }
+ };
+ Signal.init(o);
+ expect(o.signalA).toEqual(jasmine.any(Signal));
+ expect(o.signalB).toEqual(jasmine.any(Signal));
+ expect(o['signalC']).toBeUndefined();
+ });
+});
+
+describe("Signaling", function () {
var s,
callbacks = [],
status = [];
var getCallback = function (i) {
if (!callbacks[i]) {
- callbacks[i] = function () {
- status[i] = (status[i] || 0) + 1;
+ callbacks[i] = function (arg) {
+ var increment = arg || 1;
+ status[i] = (status[i] || 0) + increment;
};
}
return callbacks[i];
};
beforeEach(function () {
- s = new Signal();
+ s = new Signal;
callbacks = [];
status = [];
});
@@ -44,6 +60,14 @@ describe("Signal", function () {
expect(status[0]).toEqual(1);
});
+ it("signal should pass along args", function () {
+ s.add(getCallback(0));
+ s.signal(5);
+ expect(status[0]).toEqual(5);
+ s.signal(2);
+ expect(status[0]).toEqual(7);
+ });
+
it("should handle complex sequences of add/remove/signal", function () {
var testCases = [
0, [], // No-op.
View
8 src/always/Signal.js
@@ -1,6 +1,12 @@
var Signal = function() {
};
+Signal.init = function(obj) {
+ for (var signal in obj.signals) {
+ obj[signal] = new Signal;
+ }
+};
+
Signal.prototype = {
add: function(callback) {
if (!this._callback) {
@@ -33,7 +39,7 @@ Signal.prototype = {
var i = 0;
var listener = listeners[i] || listeners;
while (listener) {
- listener.call(this, arguments);
+ listener.apply(this, arguments);
listener = listeners[++i];
}
}

0 comments on commit 653992a

Please sign in to comment.