| 191 | } |
| 192 | |
| 193 | class MixedSchedulerDispatcher implements SchedulerDispatcher { |
| 194 | private tasks = new PriorityBuckets() |
| 195 | private running: (() => void) | undefined = undefined |
| 196 | readonly setImmediate: (f: () => void) => () => void |
| 197 | |
| 198 | constructor( |
| 199 | setImmediateFn: (f: () => void) => () => void = setImmediate |
| 200 | ) { |
| 201 | this.setImmediate = setImmediateFn |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @since 2.0.0 |
| 206 | */ |
| 207 | scheduleTask(task: () => void, priority: number) { |
| 208 | this.tasks.scheduleTask(task, priority) |
| 209 | if (this.running === undefined) { |
| 210 | this.running = this.setImmediate(this.afterScheduled) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @since 2.0.0 |
| 216 | */ |
| 217 | afterScheduled = () => { |
| 218 | this.running = undefined |
| 219 | this.runTasks() |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @since 2.0.0 |
| 224 | */ |
| 225 | runTasks() { |
| 226 | const buckets = this.tasks.drain() |
| 227 | for (let i = 0; i < buckets.length; i++) { |
| 228 | const toRun = buckets[i][1] |
| 229 | for (let j = 0; j < toRun.length; j++) { |
| 230 | toRun[j]() |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @since 2.0.0 |
| 237 | */ |
| 238 | flush() { |
| 239 | while (this.tasks.buckets.length > 0) { |
| 240 | if (this.running !== undefined) { |
| 241 | this.running() |
| 242 | this.running = undefined |
| 243 | } |
| 244 | this.runTasks() |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Context reference that controls the maximum number of operations a fiber |