MCPcopy Create free account
hub / github.com/Effect-TS/effect / TestExecutor

Class TestExecutor

packages/effect/src/ui/internal/executor.ts:55–144  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53 * @internal
54 */
55export class TestExecutor implements Executor, Scheduler.SchedulerDispatcher {
56 private tasks: Array<Task> = []
57 /** How many tasks have run. */
58 ran = 0
59 private random: number
60 readonly flushes: Flush
61 readonly scheduler: Scheduler.Scheduler
62 constructor(flushes: Flush = "queued") {
63 this.flushes = flushes
64 this.random = typeof flushes === "string" ? 0 : flushes.seed >>> 0
65 this.scheduler = {
66 executionMode: "sync",
67 shouldYield: (fiber) => fiber.currentOpCount >= fiber.cache.maxOpsBeforeYield,
68 makeDispatcher: () => this
69 }
70 }
71 schedule(task: () => void): void {
72 const flushes = this.flushes
73 if (flushes === "queued") return this.scheduleTask(task, 0)
74 // Outside the priorities: every task goes behind `-Infinity` and ahead of `Infinity`, whenever it's queued.
75 if (typeof flushes === "string") {
76 return this.scheduleTask(task, flushes === "first" ? -Infinity : Infinity)
77 }
78 this.tasks.splice(this.next(this.tasks.length + 1), 0, {
79 task,
80 priority: -Infinity
81 })
82 }
83 /** mulberry32: the same seed gives the same places. */
84 private next(below: number): number {
85 let t = this.random = (this.random + 0x6D2B79F5) >>> 0
86 t = Math.imul(t ^ (t >>> 15), t | 1)
87 t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
88 return Math.floor((((t ^ (t >>> 14)) >>> 0) / 4294967296) * below)
89 }
90 scheduleTask(task: () => void, priority: number): void {
91 this.queue({ task, priority })
92 }
93 private queue(entry: Task): void {
94 let index = this.tasks.length
95 while (index > 0 && this.tasks[index - 1]!.priority > entry.priority) {
96 index--
97 }
98 this.tasks.splice(index, 0, entry)
99 }
100 /**
101 * The Scheduler for fibers outside the tree. Their tasks join the queue, so
102 * a drain runs them in turn with the tree's, and each also gets a task on a
103 * dispatcher of `wake`, which runs it if no drain has.
104 */
105 outside(wake: Scheduler.Scheduler): Scheduler.Scheduler {
106 const woken = wake.makeDispatcher()
107 const dispatcher: Scheduler.SchedulerDispatcher = {
108 scheduleTask: (task, priority) => {
109 const entry: Task = { task, priority }
110 this.queue(entry)
111 woken.scheduleTask(() => {
112 // Gone when a drain ran it first.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected