{
  "version": 3,
  "sources": ["../src/main.ts"],
  "sourcesContent": ["import {newFlatomise} from '@alwatr/flatomise';\n\nimport type {} from '@alwatr/type-helper';\n\n/**\n * A queue that executes async tasks in order like mutex and semaphore methodology\n *\n * @example\n * ```ts\n * const queue = new AsyncQueue();\n *\n * function longTask() {\n *   queue.push('longTaskId', async () => {\n *     // ...\n *   });\n * }\n * ```\n */\nexport class AsyncQueue {\n  /**\n   * A record of task IDs and their corresponding last queued task promises.\n   */\n  private queue__: DictionaryOpt<Promise<unknown>> = {};\n\n  /**\n   * Push a async task to the queue.\n   *\n   * @param taskId task id\n   * @param task async task\n   * @returns A promise that resolves when the task is done.\n   *\n   * @example\n   * ```typescript\n   * const queue = new AsyncQueue();\n   *\n   * function longTask() {\n   *  queue.push('longTaskId', async () => {\n   *   // ...\n   * });\n   * ```\n   */\n  public async push<T>(taskId: string, task: () => Promise<T>): Promise<T> {\n    const flatomise = newFlatomise<T>();\n\n    const previousTaskPromise = this.queue__[taskId];\n    this.queue__[taskId] = flatomise.promise;\n\n    try {\n      await previousTaskPromise;\n    }\n    catch (_e) {\n      // ignore\n    }\n\n    setTimeout(() => {\n      task()\n        .then(flatomise.resolve, flatomise.reject)\n        .then(() => {\n          if (this.queue__[taskId] === flatomise.promise) {\n            delete this.queue__[taskId];\n          }\n        });\n    }, 0);\n\n    return flatomise.promise;\n  }\n\n  /**\n   * Check if the task running in the queue.\n   *\n   * @param taskId task id\n   * @returns true if the task is running, otherwise false.\n   * @example\n   * ```typescript\n   * if (queue.isRunning('longTaskId')) {\n   *  // ...\n   * }\n   * ```\n   */\n  public isRunning(taskId: string): boolean {\n    return this.queue__[taskId] !== undefined;\n  }\n\n  /**\n   * Wait for the all tasks in the queue to finish.\n   *\n   * @param taskId task id\n   * @returns A promise that resolves when all tasks are done.\n   * @example\n   * ```typescript\n   * await queue.waitForFinish('longTaskId');\n   * ```\n   */\n  public waitForFinish(taskId: string): Promise<unknown> {\n    return this.queue__[taskId] ?? Promise.resolve();\n  }\n\n  /**\n   * Wait for the all tasks in the queue to finish.\n   * @returns A promise that resolves when all tasks are done.\n   * @example\n   * ```typescript\n   * await queue.waitForAllFinish();\n   * ```\n   */\n  public waitForAllFinish(): Promise<unknown[]> {\n    return Promise.all(Object.values(this.queue__));\n  }\n}\n"],
  "mappings": ";;qqBAAA,sIAA2B,6BAkBpB,IAAM,WAAN,KAAiB,CAAjB,cAIL,KAAQ,QAA2C,CAAC,EAmBpD,MAAa,KAAQ,OAAgB,KAAoC,CACvE,MAAM,aAAY,+BAAgB,EAElC,MAAM,oBAAsB,KAAK,QAAQ,MAAM,EAC/C,KAAK,QAAQ,MAAM,EAAI,UAAU,QAEjC,GAAI,CACF,MAAM,mBACR,OACO,GAAI,CAEX,CAEA,WAAW,IAAM,CACf,KAAK,EACF,KAAK,UAAU,QAAS,UAAU,MAAM,EACxC,KAAK,IAAM,CACV,GAAI,KAAK,QAAQ,MAAM,IAAM,UAAU,QAAS,CAC9C,OAAO,KAAK,QAAQ,MAAM,CAC5B,CACF,CAAC,CACL,EAAG,CAAC,EAEJ,OAAO,UAAU,OACnB,CAcO,UAAU,OAAyB,CACxC,OAAO,KAAK,QAAQ,MAAM,IAAM,MAClC,CAYO,cAAc,OAAkC,CACrD,OAAO,KAAK,QAAQ,MAAM,GAAK,QAAQ,QAAQ,CACjD,CAUO,kBAAuC,CAC5C,OAAO,QAAQ,IAAI,OAAO,OAAO,KAAK,OAAO,CAAC,CAChD,CACF",
  "names": []
}
