Skip to content

Commit d1784fb

Browse files
author
Web Advanced
committed
Added spec to cover subscribing arrays
1 parent 904893e commit d1784fb

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

dev/signals.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,24 @@ var signals = (function (global, undefined) {
5454
* @private
5555
* @name subscribeToSignal
5656
* @param {string} [signalName] Name of the signal. (default = 'any').
57-
* @param {Function} [fn] Callback function bound to the signal.
57+
* @param {Object} [callbacks] Callback function or Array of functions to be bound to the signal.
5858
* @param {Object} [context] Context on which listener will be executed (object that should represent the `this`. (default = window)
5959
*/
60-
subscribeToSignal = function (signalName, fn, context) {
60+
subscribeToSignal = function (signalName, callbacks, context) {
6161
var signalType = getSignalType(signalName),
6262
i = 0,
63-
l;
64-
if (fn instanceof Array) {
65-
l = fn.length;
66-
for (i; i < l; i++){
67-
checkArgument(fn, FUNCTION, 'Callback must be a function');
68-
evts[signalType].push({callback: fn[i], obj: context || global});
63+
l,
64+
callback;
65+
if (callbacks instanceof Array) {
66+
l = callbacks.length;
67+
for (i; i < l; i++) {
68+
callback = callbacks[i];
69+
checkArgument(callback, FUNCTION, 'Callback must be a function');
70+
evts[signalType].push({callback: callback, obj: context || global});
6971
}
7072
} else {
71-
checkArgument(fn, FUNCTION, 'Callback must be a function');
72-
evts[signalType].push({callback: fn, obj: context || global});
73+
checkArgument(callbacks, FUNCTION, 'Callback must be a function');
74+
evts[signalType].push({callback: callbacks, obj: context || global});
7375
}
7476
};
7577
/**

dev/specs/signals.specs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ describe("Signals JS", function () {
1010
flag_2 = arg.flag_2;
1111
mockString = arg.mockString;
1212
}
13+
function func_1() {};
14+
function func_2() {};
15+
function func_3() {};
1316
beforeEach(function () {
1417
falg_1 = false;
1518
falg_2 = false;
@@ -29,6 +32,11 @@ describe("Signals JS", function () {
2932
expect(signals.isObservable('mock:event')).toEqual(true);
3033
});
3134

35+
it("should be able to subscribe a collection of funcs to mock:collection", function() {
36+
signals.subscribe('mock:collection', [func_1, func_2, func_3]);
37+
expect(signals.listenerCount('mock:collection')).toEqual(3);
38+
});
39+
3240
it("should throw when adding anything other then a function as the callback", function() {
3341
var failingFunc = function () {
3442
signals.subscribe('mock:event', 'fakeFunc');

0 commit comments

Comments
 (0)