| 106 | buckets: Array<[priority: number, tasks: Array<() => void>]> = [] |
| 107 | |
| 108 | scheduleTask(task: () => void, priority: number): void { |
| 109 | const buckets = this.buckets |
| 110 | const len = buckets.length |
| 111 | let bucket: [number, Array<() => void>] | undefined |
| 112 | let index = 0 |
| 113 | for (; index < len; index++) { |
| 114 | if (buckets[index][0] > priority) break |
| 115 | bucket = buckets[index] |
| 116 | } |
| 117 | if (bucket && bucket[0] === priority) { |
| 118 | bucket[1].push(task) |
| 119 | } else if (index === len) { |
| 120 | buckets.push([priority, [task]]) |
| 121 | } else { |
| 122 | buckets.splice(index, 0, [priority, [task]]) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | drain() { |
| 127 | const buckets = this.buckets |