All files index.js

88.35% Statements 91/103
81.25% Branches 39/48
80.95% Functions 17/21
89% Lines 89/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  6x 6x 6x 6x 6x       23x 23x 17x   6x 6x                   12x 12x   12x 12x     1x 6x     1x 23x 23x 23x 23x 17x   6x 6x   6x       1x 2x     1x 1x   24x   1x                   1x 10x 12x 29x 29x 29x 29x 29x 24x 24x 2x 22x 4x 3x     18x     26x   26x 20x 20x 20x 19x 17x     1x     6x 6x     26x 26x 26x       1x       1x 8x       8x 17x         1x 2x       2x 2x 12x 12x 4x 4x   12x       1x 10x   10x 13x 9x 8x 1x         9x     4x 2x 2x 2x 1x         3x 3x               3x      
function Deferred(fn, thisArg, args, resolve, reject) {
  this.args = args;
  this.fn = fn;
  this.reject = reject;
  this.resolve = resolve;
  this.thisArg = thisArg;
}
 
function next() {
  const d = this.pop();
  if (d === undefined) {
    ++this.concurrency;
  } else {
    try {
      d.resolve(d.fn.apply(d.thisArg, d.args));
    } catch (error) {
      d.reject(error);
    }
  }
}
 
// based on implementation in https://github.com/ForbesLindesay/throat
function Queue(concurrency) {
  // not related to the queue implementation but used in this lib
  this.concurrency = concurrency;
  this.next = next.bind(this);
 
  this._s1 = []; // stack to push to
  this._s2 = []; // stack to pop from
}
 
Queue.prototype.push = function(value) {
  this._s1.push(value);
};
 
Queue.prototype.pop = function() {
  let s2 = this._s2;
  Eif (s2.length === 0) {
    const s1 = this._s1;
    if (s1.length === 0) {
      return;
    }
    this._s1 = s2;
    s2 = this._s2 = s1.reverse();
  }
  return s2.pop();
};
 
const getSymbol =
  typeof Symbol === "function"
    ? key => Symbol.for("limit-concurrency-decorator/" + key)
    : key => "@@limit-concurrency-decorator/" + key;
 
export const FAIL_ON_QUEUE = getSymbol("FAIL_ON_QUEUE");
export const BYPASS_QUEUE = getSymbol("BYPASS_QUEUE");
 
const defaultTermination = promise => promise;
 
const { slice } = Array.prototype;
 
function callWrapper(fn) {
  if (typeof fn !== "function") {
    fn = this[fn];
  }
 
  return fn.apply(this, slice.call(arguments, 1));
}
 
const makeLimiter = (getQueue, termination = defaultTermination) => {
  return (fn = callWrapper) =>
    function() {
      const queue = getQueue(this);
      let canRun = queue.concurrency > 0;
      let argStart = 0;
      const { length } = arguments;
      if (argStart < length) {
        const maybeFlag = arguments[argStart++];
        if (maybeFlag === BYPASS_QUEUE) {
          canRun = true;
        } else if (maybeFlag === FAIL_ON_QUEUE) {
          if (!canRun) {
            return Promise.reject(new Error("no available place in queue"));
          }
        } else {
          --argStart;
        }
      }
      const args = slice.call(arguments, argStart);
      let promise;
      if (canRun) {
        --queue.concurrency;
        try {
          promise = fn.apply(this, args);
          if (promise == null || typeof promise.then !== "function") {
            promise = Promise.resolve(promise);
          }
        } catch (error) {
          promise = Promise.reject(error);
        }
      } else {
        promise = new Promise((resolve, reject) =>
          queue.push(new Deferred(fn, this, args, resolve, reject))
        );
      }
      const { next } = queue;
      termination(promise).then(next, next);
      return promise;
    };
};
 
const identity = fn => fn;
 
// create a function limiter where the concurrency is shared between
// all functions
const limitFunction = (concurrency, opts) => {
  Iif (concurrency === 0 || concurrency === Infinity) {
    return identity;
  }
 
  const queue = new Queue(concurrency);
  return makeLimiter(() => queue, opts);
};
 
// create a method limiter where the concurrency is shared between all
// methods but locally to the instance
export const limitMethod = (concurrency, opts) => {
  Iif (concurrency === 0 || concurrency === Infinity) {
    return identity;
  }
 
  const queues = new WeakMap();
  return makeLimiter(obj => {
    let queue = queues.get(obj);
    if (queue === undefined) {
      queue = new Queue(concurrency);
      queues.set(obj, queue);
    }
    return queue;
  }, opts);
};
 
export const limitConcurrency = (...args) => {
  let method = false;
  let wrap;
  return (target, key, descriptor) => {
    if (key === undefined) {
      if (wrap === undefined) {
        wrap = limitFunction(...args);
      } else Iif (method) {
        throw new Error(
          "the same decorator cannot be used between function and method"
        );
      }
      return wrap(target);
    }
 
    if (wrap === undefined) {
      method = true;
      wrap = limitMethod(...args);
    } else if (!method) {
      throw new Error(
        "the same decorator cannot be used between function and method"
      );
    }
 
    Eif ("value" in descriptor) {
      descriptor.value = wrap(descriptor.value);
    } else {
      const { get } = descriptor;
      descriptor.get = function() {
        return wrap(get.call(this));
      };
    }
 
    return descriptor;
  };
};