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MAAM,CAAC,MAAP,GAAgB,CAApB,EAAuB;AAC1B,UAAM,KAAI,sBAAJ,EAAgB,MAAhB,EAAwB,kBAAxB,CAAN;AACD;AACF,C","sourcesContent":["import { CancellationToken } from \"builder-util-runtime\"\nimport { log } from \"./log\"\nimport { NestedError } from \"./promise\"\n\nexport class AsyncTaskManager {\n  readonly tasks: Array<Promise<any>> = []\n  private readonly errors: Array<Error> = []\n\n  constructor(private readonly cancellationToken: CancellationToken) {\n  }\n\n  add(task: () => Promise<any>) {\n    if (this.cancellationToken == null || !this.cancellationToken.cancelled) {\n      this.addTask(task())\n    }\n  }\n\n  addTask(promise: Promise<any>) {\n    if (this.cancellationToken.cancelled) {\n      log.debug({reason: \"cancelled\", stack: new Error().stack}, \"async task not added\")\n      if (\"cancel\" in promise) {\n        (promise as any).cancel()\n      }\n      return\n    }\n\n    this.tasks.push(promise\n      .catch(it => {\n        log.debug({error: it.message || it.toString()}, \"async task error\")\n        this.errors.push(it)\n        return Promise.resolve(null)\n      }))\n  }\n\n  cancelTasks() {\n    for (const task of this.tasks) {\n      if (\"cancel\" in task) {\n        (task as any).cancel()\n      }\n    }\n    this.tasks.length = 0\n  }\n\n  async awaitTasks(): Promise<Array<any>> {\n    if (this.cancellationToken.cancelled) {\n      this.cancelTasks()\n      return []\n    }\n\n    const checkErrors = () => {\n      if (this.errors.length > 0) {\n        this.cancelTasks()\n        throwError(this.errors)\n        return\n      }\n    }\n\n    checkErrors()\n\n    let result: Array<any> | null = null\n    const tasks = this.tasks\n    let list = tasks.slice()\n    tasks.length = 0\n    while (list.length > 0) {\n      const subResult = await Promise.all(list)\n      result = result == null ? subResult : result.concat(subResult)\n      checkErrors()\n      if (tasks.length === 0) {\n        break\n      }\n      else {\n        if (this.cancellationToken.cancelled) {\n          this.cancelTasks()\n          return []\n        }\n\n        list = tasks.slice()\n        tasks.length = 0\n      }\n    }\n    return result || []\n  }\n}\n\nfunction throwError(errors: Array<Error>) {\n  if (errors.length === 1) {\n    throw errors[0]\n  }\n  else if (errors.length > 1) {\n    throw new NestedError(errors, \"Cannot cleanup: \")\n  }\n}"],"sourceRoot":""}