Code coverage report for lib/twoStep.js

Statements: 100% (82 / 82)      Branches: 100% (12 / 12)      Functions: 100% (22 / 22)      Lines: 100% (80 / 80)      Ignored: none     

All files » lib/ » twoStep.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                  1                 1 43 43 43 43     1 43 42 42 42     1 11             1 72 72           1 69 69 32                     1 72 72 72 72 72 3   69                 1 4 4   4 4   4 4           1 18 18 19                                   1 21 21 21 21 21 1   20                 1 6 6   6             1 20   1 58 19   39 39 39 39   8     20       1 23 22 18       1 15             1 2 20 20 20       1 1 1 1   1 1   1    
'use strict';
 
/**
 * Twostep - a simple flow-control library for node.js.
 *
 * Steppy - even more simple in usage tool covering majority
 * of the everyday routine use-cases.
 */
 
var slice = Array.prototype.slice;
 
/**
 * Group represents one step in Step's execution flow.
 * Makes a decision when the execution should jump to the next step.
 *
 * @param {function(err, args...)} callback
 *   Callback to be called when all data is collected or an error occured.
 */
function Group(callback) {
	this.slots = [null];
	this.reservedSlots = 0;
	this.callback = callback;
	this.isDone = false;
}
 
Group.prototype.done = function done(err) {
	if (this.isDone) return;
	this.isDone = true;
	this.slots[0] = err;
	this.callback.apply(null, this.slots);
};
 
Group.prototype.error = function error(err) {
	this.done(err);
};
 
/**
 * Reserve space for one argument in the `slots` array.
 * @return {Number} index of the reserved slot
 */
Group.prototype._reserveSlot = function() {
	this.reservedSlots++;
	return this.slots.push(undefined) - 1;
};
 
/**
 * Fill the reserved slot addressed by `index` with the given `value`.
 */
Group.prototype._fillSlot = function(index, data) {
	this.slots[index] = data;
	if (!--this.reservedSlots) {
		this.done();
	}
};
 
/**
 * Reserve one slot in the arguments array to be passed
 * to the group's callback. Group's callback will not be called until
 * all reserved slots are filled with data or the error occures.
 *
 * @return {function(err, data)} callback to fill the slot with data
 */
Group.prototype.slot = function slot() {
	var self = this;
	var index = self._reserveSlot();
	return function(err, data) {
		process.nextTick(function() {
			if (err) {
				return self.error(err);
			}
			self._fillSlot(index, data);
		});
	};
};
 
/**
 * Creates a nested group, all results of which will be passed into the reserved
 * slot as a single array
 */
Group.prototype.makeGroup = function makeGroup() {
	var callback = this.slot();
	var newGroup = new Group(function (err) {
		// omit first 2 elements because of fake element
		var data = slice.call(arguments, 2);
		callback(err, data);
	});
	newGroup.slot()(null, null);
	return newGroup;
};
 
/**
 * Wrapper for passing synchronous values to the next step
 */
Group.prototype.pass = function pass(/*values*/) {
	var values = slice.call(arguments);
	for (var i = 0, l = values.length; i < l; i++) {
		this.slot()(null, values[i]);
	}
};
 
 
/**
 * TwoStep section
 */
 
/**
 * Chaining together all passed functions.
 * The results of the step call are passed to the following step
 * in the form of stepN(err, args...).
 * The execution result of the whole chain is passed
 * via the callback call: callback(err, result).
 *
 * @return {function(args..., callback)} the resulting chain of steps
 */
function chainSteps(/*fun1, fun2, ..., funN*/) {
	var steps = slice.call(arguments);
	return function(/*arg1, ... argN, callback*/) {
		var initArgs = slice.call(arguments);
		var callback = initArgs.pop();
		if (!callback) {
			throw new Error('Callback is missing');
		}
		iterateSteps(steps, initArgs, callback);
	};
};
 
/**
 * Similar to `makeSteps` but the chaining continues only on
 * successful execution of the previous step and breaks after
 * the first error occured.
 */
function chainStepsNoError() {
	var steps = slice.call(arguments).map(function(step) {
		return notHandlingError(step);
	});
	return chainSteps.apply(null, steps);
};
 
 
/**
 * The heart of the TwoStep, function executing and chaining all given steps
 */
function iterateSteps(steps, initArgs, callback) {
	var pos = 0;
 
	function next(/*err, args...*/) {
		if (pos >= steps.length) {
			return callback.apply(null, arguments);
		}
		var step = steps[pos++];
		var group = new Group(next);
		try {
			step.apply(group, arguments);
		} catch (e) {
			return group.error(e);
		}
	};
	next.apply(null, initArgs);
}
 
 
function notHandlingError(func) {
    return function(err, args) {
		if (err) throw err;
		return func.apply(this, arguments);
	};
}
 
function identity(arg) {
    return arg;
}
 
/**
 * Chain and execute given steps immediately. The last step in chain
 * will be the error- and result-handling callback.
 */
function chainAndCall(chainer) {
    return function(/*step1, step2, ...*/) {
        var steps = slice.call(arguments);
	    var callback = steps.pop();
	    chainer.apply(null, steps)(callback);
    };
}
 
exports.Step = chainAndCall(chainSteps);
exports.Step.fn = chainSteps;
exports.Step.simple = identity;
exports.Step.throwIfError = notHandlingError;
 
exports.Steppy = chainAndCall(chainStepsNoError);
exports.Steppy.fn = chainStepsNoError;
 
exports.Group = Group;