Code coverage report for lib/deprecated-chain.js

Statements: 68.75% (44 / 64)      Branches: 56.25% (18 / 32)      Functions: 55.56% (10 / 18)      Lines: 70% (42 / 60)     

All files » lib/ » deprecated-chain.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 1281 1   1   1 12 12       12   12 8           12 8 40       32 32 32       32 28 32         8       8 8         40       8                                       12     12 2448 40 40         12 2460 2448       12       1         1             1       1         1 4 4     1   8 48        
var async = require("async");
var _ = require("lodash");
 
var deprecatedChain ={};
 
deprecatedChain.chain = function(obj){
  var _this = this;
  Eif (!obj) { obj = {}; }
 
  // Update the onError callback if supplied.  The most recent .chain()
  // invocation overrides previous onError handlers.
  Iif (obj.onError) {
    this._chainOnErrorCallback = obj.onError;
  } else if (!this._chainOnErrorCallback) {
    this._chainOnErrorCallback = function(err) {
      if (err) { console.error("a function in your .chain() failed:", err); }
    };
  }
 
  // Add queue if not already here
  if(!_this._queue){
    _this._queue = async.queue(function (task, callback) {
      if(task.args.length > 0 && typeof task.args[task.args.length-1] === "function"){
        //wrap the existing callback
        //if this is queueAddAsync, we instead create a callback that will be
        //passed through to the function provided
        var cb_arg = (task.name === 'queueAddAsync' ? 1 : task.args.length - 1);
        var func = task.args[cb_arg];
        task.args[cb_arg] = function(err) {
          // if the chain user has their own callback, we will not invoke
          // the onError handler, supplying your own callback suggests you
          // handle the error on your own.
          if (func)
            { func.apply(null, arguments); }
          Eif (!_this._chainHalted) { callback(err); }
        };
      } else {
        // if the .chain() does not supply a callback, we assume they
        // expect us to catch errors.
        task.args.push(function(err) {
          // if there is an error, call the onError callback,
          // and do not invoke callback() which would make the
          // task queue continue processing
          Iif (err) { _this._chainOnErrorCallback(err); }
          else { callback(); }
        });
      }
 
      //call the function
      _this[task.name].apply(_this, task.args);
    }, 1);
 
    // add unshift method if we need to add sth to the queue
    _this._queue = _.extend(_this._queue, {
      unshift: function (data, callback) {
        var _this = this;
        if(data.constructor !== Array) {
            data = [data];
        }
        data.forEach(function(task) {
            _this.tasks.unshift({
                data: task,
                callback: typeof callback === 'function' ? callback : null
            });
            if (_this.saturated && _this.tasks.length === _this.concurrency) {
                _this.saturated();
            }
            async.nextTick(_this.process);
        });
      }
    });
  }
 
  var chain = {};
 
  //builds a placeHolder functions
  var buildPlaceholder = function(name){
    return function(){
      _this._queue.push({name: name, args: Array.prototype.slice.call(arguments, 0)});
      return chain;
    };
  };
 
  //fill the chain with placeholders
  _.each(_.functions(_this), function(k) {
    if(k !== "chain"){
      chain[k] = buildPlaceholder(k);
    }
  });
 
  return chain;
};
 
// manually stop processing of queued chained functions
deprecatedChain.haltChain = function(){
  this._chainHalted = true;
  this._queue = null;
};
 
deprecatedChain.pauseChain = function(timeoutMs, cb){
  setTimeout(function() {
    cb();
  }, timeoutMs);
  return this.chain;
};
 
deprecatedChain.next = function(){
  this._queue.unshift({name: arguments[0], args: _.rest(arguments)});
};
 
deprecatedChain.queueAdd = function(func){
  func();
  return this.chain;
};
 
deprecatedChain.queueAddAsync = function(func, cb) {
  func(cb);
  return this.chain;
};
 
module.exports = {
  patch: function(browser) {
    _(deprecatedChain).methods().each(function(methodName) {
      browser[methodName] = deprecatedChain[methodName].bind(browser);
    });
  }
};