| 150 | * @since 2.0.0 |
| 151 | */ |
| 152 | export class MixedScheduler implements Scheduler { |
| 153 | readonly executionMode: "sync" | "async" |
| 154 | readonly setImmediate: (f: () => void) => () => void |
| 155 | |
| 156 | constructor( |
| 157 | executionMode: "sync" | "async" = "async", |
| 158 | setImmediateFn?: (f: () => void) => () => void |
| 159 | ) { |
| 160 | this.executionMode = executionMode |
| 161 | this.setImmediate = setImmediateFn ?? (executionMode === "sync" ? setMicrotask : setImmediate) |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns whether the fiber has reached its operation budget and should yield. |
| 166 | * |
| 167 | * **When to use** |
| 168 | * |
| 169 | * Use to decide whether a fiber should yield after consuming its current |
| 170 | * operation budget. |
| 171 | * |
| 172 | * @since 2.0.0 |
| 173 | */ |
| 174 | shouldYield(fiber: Fiber.Fiber<unknown, unknown>) { |
| 175 | return fiber.currentOpCount >= fiber.maxOpsBeforeYield |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Creates a dispatcher that schedules work through this scheduler. |
| 180 | * |
| 181 | * **When to use** |
| 182 | * |
| 183 | * Use when you need a standalone dispatcher from a scheduler instance, for |
| 184 | * example in tests that enqueue tasks and then flush them deterministically. |
| 185 | * |
| 186 | * @since 4.0.0 |
| 187 | */ |
| 188 | makeDispatcher() { |
| 189 | return new MixedSchedulerDispatcher(this.setImmediate) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | class MixedSchedulerDispatcher implements SchedulerDispatcher { |
| 194 | private tasks = new PriorityBuckets() |
nothing calls this directly
no outgoing calls
no test coverage detected