| 19 | } |
| 20 | |
| 21 | export class IdleScheduler { |
| 22 | private queue: IdleTask[] = []; |
| 23 | private scheduled: ScheduledRun | null = null; |
| 24 | empty: Promise<void> = Promise.resolve(); |
| 25 | private emptyResolve: Function | null = null; |
| 26 | lastTrigger: number | null = null; |
| 27 | lastRun: number | null = null; |
| 28 | oldestScheduledAt: number | null = null; |
| 29 | |
| 30 | constructor( |
| 31 | private adapter: Adapter, |
| 32 | private delay: number, |
| 33 | private maxDelay: number, |
| 34 | private debug: DebugLogger, |
| 35 | ) {} |
| 36 | |
| 37 | async trigger(): Promise<void> { |
| 38 | this.lastTrigger = this.adapter.time; |
| 39 | if (this.queue.length === 0) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (this.scheduled !== null) { |
| 44 | this.scheduled.cancel = true; |
| 45 | } |
| 46 | |
| 47 | const scheduled = { |
| 48 | cancel: false, |
| 49 | }; |
| 50 | this.scheduled = scheduled; |
| 51 | |
| 52 | // Ensure that no task remains pending for longer than `this.maxDelay` ms. |
| 53 | const now = this.adapter.time; |
| 54 | const maxDelay = Math.max(0, (this.oldestScheduledAt ?? now) + this.maxDelay - now); |
| 55 | const delay = Math.min(maxDelay, this.delay); |
| 56 | |
| 57 | await this.adapter.timeout(delay); |
| 58 | |
| 59 | if (scheduled.cancel) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | this.scheduled = null; |
| 64 | |
| 65 | await this.execute(); |
| 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) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…