Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.2.1 - context support

  • Loading branch information...
commit bcc6a96a1f2ece633338a5837ff2c7300d0de91e 1 parent 010af7f
@tim-smart authored
Showing with 27 additions and 12 deletions.
  1. +20 −10 lib/parallel/index.js
  2. +1 −1  package.json
  3. +6 −1 test.js
View
30 lib/parallel/index.js
@@ -5,21 +5,30 @@ var Task = exports.Task = function Task(actions) {
this.length = 0;
if ('object' === typeof actions) {
- var keys = Object.keys(actions);
+ var keys = Object.keys(actions),
+ action;
for (var i = 0, key; key = keys[i++]; ) {
- this.add(key, actions[key]);
+ action = actions[key];
+ if (action.action) {
+ this.add(key, action.action, action.context);
+ } else {
+ this.add(key, action);
+ }
}
}
return this;
};
-Task.prototype.add = function add(name, action) {
+Task.prototype.add = function add(name, action, context) {
if (!this.actions[name]) {
++this.length;
}
- this.actions[name] = action;
+ this.actions[name] = {
+ action: action,
+ context: context || null
+ };
return this;
};
@@ -29,7 +38,7 @@ Task.prototype.bind = function bind() {
names = Array.prototype.slice.call(arguments, 0, -1);
for (var i = 0, name; name = names[i++]; ) {
- this._emitter.addListener(name, callback);
+ this._emitter.on(name, callback);
}
return this;
@@ -46,22 +55,23 @@ Task.prototype.run = function run(callback) {
}
keys.forEach(function (key) {
- var action = self.actions[key][0],
- args = self.actions[key].slice(1);
+ var action = self.actions[key].action[0],
+ args = self.actions[key].action.slice(1),
+ context = self.actions[key].context;
args.push(function() {
arguments = Array.prototype.slice.call(arguments);
arguments.unshift(key);
self._emitter.emit.apply(self._emitter, arguments);
- callback.apply(self, arguments);
+ callback.apply(context, arguments);
count--;
if (0 >= count) {
- callback.call(self, null);
+ callback.call(context, null);
}
});
- action.apply(null, args);
+ action.apply(context, args);
});
return this;
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "parallel",
"description": "Create tasks in node.js that run in parallel, or sequences that run tasks one after another.",
- "version": "0.2.0",
+ "version": "0.2.1",
"author": "Tim Smart",
"repository": {
"type": "git",
View
7 test.js
@@ -3,7 +3,10 @@ var fs = require('fs');
var sys = require('sys');
var task = new parallel.Task({
- 1: [fs.readFile, __filename],
+ 1: {
+ action: [fs.readFile, __filename],
+ context: fs
+ },
'tick': [process.nextTick]
});
@@ -13,6 +16,8 @@ task.bind(1, function (err, buffer) {
sys.puts('parallel-1: ' + buffer.length);
});
+console.log(task);
+
task.run(function (task_name, err, data) {
if (task_name === null) {
seq.run(function () {
Please sign in to comment.
Something went wrong with that request. Please try again.