Navigation Menu

Skip to content

Commit

Permalink
Fixed parallel execution problem for ordering method results
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Amor Kvalheim committed Mar 25, 2010
1 parent 577394e commit 9a0ac1b
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions lib/simplifier/simplifier.js
Expand Up @@ -28,11 +28,11 @@ var SerialFlow = exports.SerialFlow = function(functions) {
this.functions = Array.isArray(functions) ? functions : Array.prototype.slice.call(arguments, 0);
}

SerialFlow.prototype.execute = function(callback) {
this.serialExecute([], this.functions.splice(0, 1)[0], callback);
SerialFlow.prototype.execute = function(callback, index) {
this.serialExecute([], this.functions.splice(0, 1)[0], callback, index);
}

SerialFlow.prototype.serialExecute = function(values, f, callback) {
SerialFlow.prototype.serialExecute = function(values, f, callback, index) {
var self = this;

// If this is a parallel flow
Expand All @@ -41,9 +41,11 @@ SerialFlow.prototype.serialExecute = function(values, f, callback) {
if(self.functions.length > 0) {
var nextFunction = self.functions.splice(0, 1)[0];
var results = Array.prototype.slice.call(arguments)[0];
self.serialExecute(results, nextFunction, callback);
self.serialExecute(results, nextFunction, callback, index);
} else {
callback(Array.prototype.slice.call(arguments));
var results = Array.prototype.slice.call(arguments);
if(index != null) results.push(index);
callback(results);
}
});
} else {
Expand All @@ -53,9 +55,11 @@ SerialFlow.prototype.serialExecute = function(values, f, callback) {
var nextFunction = self.functions.splice(0, 1)[0];
var results = Array.prototype.slice.call(arguments);
// If an error has occured terminate call stack and return
results[0] != null ? callback(results) : self.serialExecute(results, nextFunction, callback);
results[0] != null ? callback(results) : self.serialExecute(results, nextFunction, callback, index);
} else {
callback(Array.prototype.slice.call(arguments));
var results = Array.prototype.slice.call(arguments);
if(index != null) results.push(index);
callback(results);
}
});
// Execute the code
Expand All @@ -72,7 +76,6 @@ var ParallelFlow = exports.ParallelFlow = function(functions) {
this.functions = Array.isArray(functions) ? functions : Array.prototype.slice.call(arguments, 0);
this.numberOfCallsPerformed = 0;
this.results = [];
this.indexes = {};
}

ParallelFlow.prototype.execute = function(callback) {
Expand All @@ -81,21 +84,32 @@ ParallelFlow.prototype.execute = function(callback) {
for(var i = 0; i < this.functions.length; i++) {
if(this.functions[i] instanceof SerialFlow) {
this.functions[i].execute(function() {
self.results[i] = Array.prototype.slice.call(arguments)[0];
// Pop the index off the return call and save results in the array
var results = Array.prototype.slice.call(arguments)[0];
var index = results.pop();
self.results[index] = results;

// Update the number of executed functions and return if we are done
self.numberOfCallsPerformed = self.numberOfCallsPerformed + 1;
if(self.numberOfCallsPerformed >= self.functions.length) {
callback(self.results);
}
})
}, i)
} else {
try {
this.functions[i](function() {
self.results[i] = Array.prototype.slice.call(arguments);
self.numberOfCallsPerformed = self.numberOfCallsPerformed + 1;
if(self.numberOfCallsPerformed >= self.functions.length) {
callback(self.results);
}
});
// Create a wapper function to contain the call and index reference
var wrapperFunction = function(index) {
self.functions[index](function() {
self.results[index] = Array.prototype.slice.call(arguments);
self.numberOfCallsPerformed = self.numberOfCallsPerformed + 1;
if(self.numberOfCallsPerformed >= self.functions.length) {
callback(self.results);
}
});
};

// Call the wrapper function with the index
wrapperFunction(i);
} catch (err) {
self.numberOfCallsPerformed = self.numberOfCallsPerformed + 1;
self.results[i] = [err];
Expand Down

0 comments on commit 9a0ac1b

Please sign in to comment.