| 66 | } |
| 67 | |
| 68 | async execute(): Promise<void> { |
| 69 | this.lastRun = this.adapter.time; |
| 70 | while (this.queue.length > 0) { |
| 71 | const queue = this.queue; |
| 72 | this.queue = []; |
| 73 | |
| 74 | await queue.reduce(async (previous, task) => { |
| 75 | await previous; |
| 76 | try { |
| 77 | await task.run(); |
| 78 | } catch (err) { |
| 79 | this.debug.log(err as Error, `while running idle task ${task.desc}`); |
| 80 | } |
| 81 | }, Promise.resolve()); |
| 82 | } |
| 83 | |
| 84 | if (this.emptyResolve !== null) { |
| 85 | this.emptyResolve(); |
| 86 | this.emptyResolve = null; |
| 87 | } |
| 88 | this.empty = Promise.resolve(); |
| 89 | this.oldestScheduledAt = null; |
| 90 | } |
| 91 | |
| 92 | schedule(desc: string, run: () => Promise<void>): void { |
| 93 | this.queue.push({desc, run}); |