()
| 31 | } |
| 32 | |
| 33 | private tick() { |
| 34 | if (!this.running) { |
| 35 | return |
| 36 | } |
| 37 | while ( |
| 38 | this.active.length < this.currentConcurrency && |
| 39 | this.pending.length |
| 40 | ) { |
| 41 | const nextFn = this.pending.shift() |
| 42 | if (!nextFn) { |
| 43 | throw new Error('Found task that is not a function') |
| 44 | } |
| 45 | this.active.push(nextFn) |
| 46 | ;(async () => { |
| 47 | let success = false |
| 48 | let res!: T |
| 49 | let error: any |
| 50 | try { |
| 51 | res = await nextFn() |
| 52 | success = true |
| 53 | } catch (e) { |
| 54 | error = e |
| 55 | } |
| 56 | this.active = this.active.filter((d) => d !== nextFn) |
| 57 | if (success) { |
| 58 | this.onSuccesses.forEach((d) => d(res, nextFn)) |
| 59 | } else { |
| 60 | this.onErrors.forEach((d) => d(error, nextFn)) |
| 61 | } |
| 62 | this.onSettles.forEach((d) => d(res, error)) |
| 63 | this.tick() |
| 64 | })() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | add(fn: () => Promise<T> | T, { priority }: { priority?: boolean } = {}) { |
| 69 | return new Promise<any>((resolve, reject) => { |
no test coverage detected