Skip to content

Commit 7ee3987

Browse files
committed
Allow require to accept multiple arguments. Fixes #37
1 parent 669c138 commit 7ee3987

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/parallel.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,20 @@
103103
}
104104
};
105105

106-
Parallel.prototype.require = function (func) {
107-
if (typeof func === 'string') {
108-
this.requiredScripts[this.requiredScripts.length] = func;
109-
} else if (typeof func === 'function') {
110-
this.requiredFunctions[this.requiredFunctions.length] = { fn: func };
111-
} else if (typeof func === 'object') {
112-
this.requiredFunctions[this.requiredFunctions.length] = func;
106+
Parallel.prototype.require = function () {
107+
var args = Array.prototype.slice.call(arguments, 0),
108+
func;
109+
110+
for (var i = 0; i < args.length; i++) {
111+
func = args[i];
112+
113+
if (typeof func === 'string') {
114+
this.requiredScripts.push(func);
115+
} else if (typeof func === 'function') {
116+
this.requiredFunctions.push({ fn: func })
117+
} else if (typeof func === 'object') {
118+
this.requiredFunctions.push(func);
119+
}
113120
}
114121
};
115122

test/api.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,35 @@
271271
expect(result).toEqual([26, 27, 28]);
272272
});
273273
});
274+
275+
it('should accept more than one requirement', function () {
276+
var fn = function (el, amount) {
277+
return el + amount;
278+
};
279+
280+
function factorial(n) { return n < 2 ? 1 : n * factorial(n - 1); }
281+
282+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
283+
p.require({ name: 'fn', fn: fn }, factorial);
284+
285+
var done = false;
286+
var result = null;
287+
288+
runs(function () {
289+
p.map(function (el) {
290+
return fn(factorial(el), 25);
291+
}).then(function (data) {
292+
result = data;
293+
done = true;
294+
});
295+
});
296+
297+
waitsFor(function () {
298+
return done;
299+
}, "it should finish", 500);
300+
301+
runs(function () {
302+
expect(result).toEqual([26, 27, 31]);
303+
});
304+
});
274305
});

0 commit comments

Comments
 (0)