Open History to see the last commit
1/**
2 * DrainableWorker - A queue-based worker that exposes a `drain()` effect.
3 *
4 * Wraps the common `Queue.unbounded` + `Effect.forever` pattern and adds
5 * a signal that resolves when the queue is empty **and** the current item
6 * has finished processing. This lets tests replace timing-sensitive
7 * `Effect.sleep` calls with deterministic `drain()`.
8 *
9 * @module DrainableWorker
10 */
11import { Deferred, Effect, Queue, Ref } from "effect";
12import type { Scope } from "effect";
13
14export interface DrainableWorker<A> {
15 /**
16 * Enqueue a work item and track it for `drain()`.
17 *
18 * This wraps `Queue.offer` so drain state is updated atomically with the
19 * enqueue path instead of inferring it from queue internals.
20 */
21 readonly enqueue: (item: A) => Effect.Effect<void>;
22
23 /**
24 * Resolves when the queue is empty and the worker is idle (not processing).
25 */
26 readonly drain: Effect.Effect<void>;
27}
28
29/**
30 * Create a drainable worker that processes items from an unbounded queue.
31 *
32 * The worker is forked into the current scope and will be interrupted when
33 * the scope closes. A finalizer shuts down the queue.
34 *
35 * @param process - The effect to run for each queued item.
36 * @returns A `DrainableWorker` with `queue` and `drain`.
37 */
38export const makeDrainableWorker = <A, E, R>(
39 process: (item: A) => Effect.Effect<void, E, R>,
40): Effect.Effect<DrainableWorker<A>, never, Scope.Scope | R> =>
41 Effect.gen(function* () {
42 const queue = yield* Queue.unbounded<A>();
43 const initialIdle = yield* Deferred.make<void>();
44 yield* Deferred.succeed(initialIdle, undefined).pipe(Effect.orDie);
45 const state = yield* Ref.make({
46 outstanding: 0,
47 idle: initialIdle,
48 });
49
50 yield* Effect.addFinalizer(() => Queue.shutdown(queue).pipe(Effect.asVoid));
51
52 const finishOne = Ref.modify(state, (current) => {
53 const remaining = Math.max(0, current.outstanding - 1);
54 return [
55 remaining === 0 ? current.idle : null,
56 {
57 outstanding: remaining,
58 idle: current.idle,
59 },
60 ] as const;
61 }).pipe(
62 Effect.flatMap((idle) =>
63 idle === null ? Effect.void : Deferred.succeed(idle, undefined).pipe(Effect.orDie),
64 ),
65 );
66
67 yield* Effect.forkScoped(
68 Effect.forever(
69 Queue.take(queue).pipe(
70 Effect.flatMap((item) => process(item).pipe(Effect.ensuring(finishOne))),
71 ),
72 ),
73 );
74
75 const enqueue: DrainableWorker<A>["enqueue"] = (item) =>
76 Effect.gen(function* () {
77 const nextIdle = yield* Deferred.make<void>();
78 yield* Ref.update(state, (current) =>
79 current.outstanding === 0
80 ? {
81 outstanding: 1,
82 idle: nextIdle,
83 }
84 : {
85 outstanding: current.outstanding + 1,
86 idle: current.idle,
87 },
88 );
89
90 const accepted = yield* Queue.offer(queue, item);
91 if (!accepted) {
92 yield* finishOne;
93 }
94 });
95
96 const drain: DrainableWorker<A>["drain"] = Ref.get(state).pipe(
97 Effect.flatMap(({ idle }) => Deferred.await(idle)),
98 );
99
100 return { enqueue, drain } satisfies DrainableWorker<A>;
101 });