{"version":3,"file":"qew.mjs","sources":["../qew.ts"],"sourcesContent":["function makeTriggerablePromise<T>(): [\n  Promise<T>,\n  (inp: T) => void,\n  (error: any) => void\n] {\n  let triggerResolveWith!: (inp: T) => void;\n  let triggerRejectWith!: (error: any) => void;\n  const promToReturn: Promise<T> = new Promise((resolve, reject) => {\n    const funcThatResolvesProm = (inp: T) => resolve(inp);\n    triggerResolveWith = funcThatResolvesProm;\n    triggerRejectWith = reject;\n  });\n  return [promToReturn, triggerResolveWith, triggerRejectWith];\n}\n\nexport class Qew {\n  private queue: (() => void)[] = [];\n  private executing = 0;\n\n  /**\n   *\n   * @param maxConcurrent how many functions can be run simultaneously\n   * @param delay how many ms to wait between when one function has resolved and\n   * the next one is run\n   */\n  constructor(\n    private maxConcurrent = 1,\n    private delay: number | (() => number) = 0\n  ) {\n    if (maxConcurrent < 1) {\n      throw new Error(\"maxConcurrent has to be 1 or higher\");\n    }\n    if (typeof delay === \"number\" && delay < 0) {\n      throw new Error(\n        \"`delay` parameter should be either a non-negative number or a function that returns one\"\n      );\n    }\n  }\n\n  /**\n   * Push another async function onto the queue\n   * @param asyncFunc the async function to push onto this queue\n   * @returns a Promise that resolves with `asyncFunc`'s resolved value –\n   * whenever `asyncFunc` has been run and resolved. Or the Promise will reject\n   * if `asyncFunc`'s Promise rejects\n   */\n  public push<T>(asyncFunc: () => Promise<T>) {\n    const [prom, resolveProm, rejectProm] = makeTriggerablePromise<T>();\n\n    const funcToRun = () => {\n      asyncFunc()\n        .then((result) => {\n          resolveProm(result);\n          this.executing = this.executing - 1;\n\n          const delay =\n            typeof this.delay === \"function\" ? this.delay() : this.delay;\n\n          setTimeout(this.tryMove, delay);\n        })\n        .catch(rejectProm);\n    };\n\n    this.queue.push(funcToRun);\n\n    this.tryMove();\n\n    return prom;\n  }\n\n  /**\n   * @deprecated this is now only an alias for `Qew#push`\n   */\n  public pushProm = this.push;\n\n  private tryMove() {\n    if (this.executing < this.maxConcurrent) {\n      const first = this.queue.shift();\n\n      if (first) {\n        this.executing = this.executing + 1;\n        first();\n      }\n    }\n  }\n}\n\nexport default Qew;\n"],"names":["Qew","constructor","maxConcurrent","delay","this","push","Error","asyncFunc","triggerResolveWith","triggerRejectWith","Promise","resolve","reject","inp","queue","then","result","resolveProm","executing","tryMove","catch","rejectProm","prom","first","shift"],"mappings":"AAeA,IAAaA,EAUXC,SACUC,EACAC,qBADgB,kBACiB,sBADjCD,aACAC,aAXsB,kBACZ,gBAwDFC,KAAKC,KA5CjBH,EAAgB,QACZ,IAAII,MAAM,0CAEG,iBAAVH,GAAsBA,EAAQ,QACjC,IAAIG,MACR,4FAYCD,YAAAA,cAAQE,OAzCXC,EACAC,WAMG,CAL0B,IAAIC,iBAASC,EAASC,GAErDJ,WAD8BK,UAAWF,EAAQE,IAEjDJ,EAAoBG,IAEAJ,EAAoBC,oCAmDnCK,MAAMT,gBAbTE,IACGQ,cAAMC,GACLC,EAAYD,KACPE,UAAYd,EAAKc,UAAY,MAE5Bf,EACkB,mBAAfC,EAAKD,MAAuBC,EAAKD,QAAUC,EAAKD,iBAE9CC,EAAKe,QAAShB,KAE1BiB,MAAMC,UAKNF,UAEEG,GAQDH,YAAAA,sBACFf,KAAKc,UAAYd,KAAKF,cAAe,KACjCqB,EAAQnB,KAAKU,MAAMU,QAErBD,SACGL,UAAYd,KAAKc,UAAY"}