| 75 | } |
| 76 | |
| 77 | private async run() { |
| 78 | if (this.running) return |
| 79 | try { |
| 80 | this.running = true |
| 81 | while (this.queue.length) { |
| 82 | const task = this.queue.shift()! |
| 83 | await task() |
| 84 | if (this.timeout) { |
| 85 | await sleep(this.timeout) |
| 86 | } |
| 87 | } |
| 88 | } finally { |
| 89 | // this try/finally should not be needed because the tasks don't throw |
| 90 | // but better safe than sorry |
| 91 | // console.log('\n\n\nrunning false\n\n\n') |
| 92 | this.running = false |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds a task to the queue and returns a promise that resolves with the task's result. |