Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address issue #41, replace process.nextTick(check) because that causes a... #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions lib/step.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ SOFTWARE.
// modified to fit my taste and the node.JS error handling system.
function Step() {
var steps = Array.prototype.slice.call(arguments),
pending, counter, results, lock;
pending, counter, results, lock,
groupLocalCallback, groupCounter, groupPending, groupResult, groupError;

// Define the main callback that's given as `this` to the steps.
function next() {
counter = pending = 0;
groupCounter = groupPending = 0;
groupLocalCallback = groupError = undefined;
groupResult = [];

// Check if there are no steps left
if (steps.length === 0) {
Expand All @@ -54,12 +58,20 @@ function Step() {
next(e);
}

if (groupCounter > 0 && groupPending == 0) { // all group() are called synchronously
groupLocalCallback(groupError, groupResult); // because groupLocalCallback() is called with lock == true, we delay next() call to next stanza
} else if (groupCounter == 0 && groupLocalCallback) { // after var group = this.group(), didn't call any group()
next(undefined, []);
lock = false;
return;
}

if (counter > 0 && pending == 0) {
// If parallel() was called, and all parallel branches executed
// synchronously, go on to the next step immediately.
next.apply(null, results);
} else if (result !== undefined) {
// If a synchronous return is used, pass it to the callback
// If a syncronous return is used, pass it to the callback
next(undefined, result);
}
lock = false;
Expand Down Expand Up @@ -87,32 +99,27 @@ function Step() {

// Generates a callback generator for grouped results
next.group = function () {
var localCallback = next.parallel();
var counter = 0;
var pending = 0;
var result = [];
var error = undefined;
groupLocalCallback = next.parallel();

function check() {
if (pending === 0) {
if (groupPending === 0) {
// When group is done, call the callback
localCallback(error, result);
groupLocalCallback(groupError, groupResult);
}
}
process.nextTick(check); // Ensures that check is called at least once

// Generates a callback for the group
return function () {
var index = counter++;
pending++;
var index = groupCounter++;
groupPending++;
return function () {
pending--;
groupPending--;
// Compress the error from any result to the first argument
if (arguments[0]) {
error = arguments[0];
groupError = arguments[0];
}
// Send the other results as arguments
result[index] = arguments[1];
groupResult[index] = arguments[1];
if (!lock) { check(); }
};
};
Expand Down Expand Up @@ -140,10 +147,9 @@ Step.fn = function StepFn() {
toRun.push(args.pop());
}


Step.apply(null, toRun);
}
}
};
};


// Hook into commonJS module systems
Expand Down