| 124 | * @category constructors |
| 125 | */ |
| 126 | export class MixedScheduler implements Scheduler { |
| 127 | private readonly getRunner = SchedulerRunner.cached((depth, drain) => { |
| 128 | if (depth >= this.maxNextTickBeforeTimer) { |
| 129 | setTimeout(() => drain(0), 0) |
| 130 | } else { |
| 131 | Promise.resolve(void 0).then(() => drain(depth + 1)) |
| 132 | } |
| 133 | }) |
| 134 | |
| 135 | constructor( |
| 136 | /** |
| 137 | * @since 2.0.0 |
| 138 | */ |
| 139 | readonly maxNextTickBeforeTimer: number |
| 140 | ) {} |
| 141 | |
| 142 | /** |
| 143 | * @since 2.0.0 |
| 144 | */ |
| 145 | shouldYield(fiber: RuntimeFiber<unknown, unknown>): number | false { |
| 146 | return fiber.currentOpCount > fiber.getFiberRef(core.currentMaxOpsBeforeYield) |
| 147 | ? fiber.getFiberRef(core.currentSchedulingPriority) |
| 148 | : false |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @since 2.0.0 |
| 153 | */ |
| 154 | scheduleTask(task: Task, priority: number, fiber?: RuntimeFiber<unknown, unknown>) { |
| 155 | this.getRunner(fiber).scheduleTask(task, priority) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @since 2.0.0 |