Created
December 19, 2023 17:36
-
-
Save wesselvdv/5bb72a6ea492a5d1491cd2776d3cbb1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FxScheduler implements Scheduler.Scheduler { | |
running = false | |
tasks = new Scheduler.PriorityBuckets() | |
constructor( | |
private readonly maxNextTickBeforeTimer: number, | |
private readonly priority: number, | |
) {} | |
private starveInternal(depth: number) { | |
const tasks = this.tasks.buckets | |
this.tasks.buckets = [] | |
for (const [_, toRun] of tasks) { | |
for (let i = 0; i < toRun.length; i++) { | |
// @ts-expect-error | |
toRun[i]() | |
} | |
} | |
if (this.tasks.buckets.length === 0) { | |
this.running = false | |
} else { | |
this.starve(depth) | |
} | |
} | |
private starve(depth = 0) { | |
if (depth >= this.maxNextTickBeforeTimer) { | |
scheduleCallback(this.priority, () => this.starveInternal(0)) | |
} else { | |
scheduleCallback(this.priority, () => this.starveInternal(depth + 1)) | |
} | |
} | |
shouldYield(fiber: Fiber.RuntimeFiber<unknown, unknown>): number | false { | |
return fiber.currentOpCount > | |
fiber.getFiberRef(FiberRef.currentMaxOpsBeforeYield) | |
? fiber.getFiberRef(FiberRef.currentSchedulingPriority) | |
: false | |
} | |
scheduleTask(task: Scheduler.Task, priority: number) { | |
this.tasks.scheduleTask(task, priority) | |
if (!this.running) { | |
this.running = true | |
this.starve() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment