Code coverage report for lib/stack.js

Statements: 100% (20 / 20)      Branches: 100% (8 / 8)      Functions: 100% (5 / 5)      Lines: 100% (19 / 19)     

All files » lib/ » stack.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          1 86 86 86 86     1   305     305   305 305   305 305 305         391 91           86 86 86       1  
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var Stack = function () {
    this.errors   = [];
    this.finished = 0;
    this.results  = [];
    this.total    = 0;
};
 
Stack.prototype = {
    add: function (fn) {
        var self  = this,
            index = self.total;
 
        self.total += 1;
 
        return function (err) {
            if (err) { self.errors[index] = err; }
 
            self.finished += 1;
            self.results[index] = fn.apply(null, arguments);
            self.test();
        };
    },
 
    test: function () {
        if (this.finished >= this.total && this.callback) {
            this.callback.call(null, this.errors.length ? this.errors : null,
                    this.results, this.data);
        }
    },
 
    done: function (callback, data) {
        this.callback = callback;
        this.data     = data;
        this.test();
    }
};
 
exports.Stack = Stack;