Code coverage report for src/lib/sequence.js

Statements: 30.3% (10 / 33)      Branches: 6.25% (1 / 16)      Functions: 16.67% (1 / 6)      Lines: 30.3% (10 / 33)      Ignored: none     

All files » src/lib/ » sequence.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  1   1       1                       1                               1                                             1       1 1   1     1    
// from: https://github.com/coolaj86/futures
;(function() {
 
  function isSequence(obj) {
    return obj instanceof Sequence;
  }
 
  function Sequence(global_context) {
    var self = this,
      waiting = true,
      data,
      stack = [];
 
    if (!isSequence(this)) {
      return new Sequence(global_context);
    }
 
    global_context = global_context || null;
 
    function next() {
      var args = Array.prototype.slice.call(arguments),
        seq = stack.shift(); // BUG this will eventually leak
 
      data = arguments;
 
      if (!seq) {
        // the chain has ended (for now)
        waiting = true;
        return;
      }
 
      args.unshift(next);
      seq.callback.apply(seq._context, args);
    }
 
    function then(callback, context) {
      if ('function' !== typeof callback) {
        throw new Error("`Sequence().then(callback [context])` requires that `callback` be a function and that `context` be `null`, an object, or a function");
      }
      stack.push({
        callback: callback,
        _context: (null === context ? null : context || global_context),
        index: stack.length
      });
 
      // if the chain has stopped, start it back up
      if (waiting) {
        waiting = false;
        next.apply(null, data);
      }
 
      return self;
    }
 
    self.next = next;
    self.then = then;
  }
 
  function createSequence(context) {
    // TODO use prototype instead of new
    return (new Sequence(context));
  }
  Sequence.create = createSequence;
  Sequence.isSequence = isSequence;
 
  Iif (typeof window === 'object')
    window.Sequence = Sequence;
  else
    module.exports = Sequence;
    
})();