@orkestrel/worker 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ import { EmitterErrorHandler } from '@orkestrel/emitter';
2
+ import { EmitterHooks } from '@orkestrel/emitter';
3
+ import { EmitterInterface } from '@orkestrel/emitter';
4
+ import { PoolOptions } from '@orkestrel/pool';
5
+ import { QueueEntryOptions } from '@orkestrel/queue';
6
+ import { QueueExecution } from '@orkestrel/queue';
7
+ import { QueueStoreInterface } from '@orkestrel/queue';
8
+
9
+ /**
10
+ * Create a resource-backed job worker — a `Queue` (`@orkestrel/queue`) marrying a `Pool`
11
+ * (`@orkestrel/pool`). Each enqueued input runs through the handler against an
12
+ * automatically acquired pooled resource (released when the job settles), with the
13
+ * queue's bounded concurrency, retries, and per-attempt timeout / abort.
14
+ *
15
+ * @remarks
16
+ * The pool's `max` defaults to `concurrency`, so resources match the jobs in flight and
17
+ * are reused across jobs. A handler that throws still releases its resource (the
18
+ * acquire/release pair brackets the call in a `finally`), so a later job reuses it. The
19
+ * lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
20
+ * delegates to the queue; `destroy` also tears the pool down. Observable (§13): a typed
21
+ * `emitter` surfaces the queue lifecycle (`enqueue` / `start` / `success` / `failure` / …).
22
+ *
23
+ * @typeParam TInput - The work input each job carries
24
+ * @typeParam TResource - The pooled resource each job runs against
25
+ * @typeParam TResult - The value the handler resolves for a job
26
+ * @param options - The `handler` and `pool` plus optional `concurrency` (default `1`),
27
+ * `retries` (default `0`), and a default per-attempt `timeout` in milliseconds
28
+ * @returns A working {@link WorkerInterface}
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { createWorker } from '@src/core'
33
+ *
34
+ * const worker = createWorker<Query, Connection, Rows>({
35
+ * pool: { create: () => connect(), destroy: (connection) => connection.close() },
36
+ * handler: (query, connection, { signal }) => connection.run(query, signal),
37
+ * concurrency: 4,
38
+ * retries: 1,
39
+ * })
40
+ *
41
+ * const rows = await worker.enqueue(query)
42
+ * ```
43
+ */
44
+ export declare function createWorker<TInput, TResource, TResult>(options: WorkerOptions<TInput, TResource, TResult>): WorkerInterface<TInput, TResult>;
45
+
46
+ /**
47
+ * A resource-backed job worker — a thin facade composing a `Queue` (`@orkestrel/queue`)
48
+ * with a `Pool` (`@orkestrel/pool`).
49
+ *
50
+ * @remarks
51
+ * - **Composition, not reimplementation.** The Worker owns a `Pool` (built from
52
+ * `options.pool`) and a `Queue` whose handler ACQUIRES a pooled resource, runs the
53
+ * user handler against it, and RELEASES it in a `finally`. All concurrency, retries,
54
+ * timeout, and lifecycle are the Queue's — the Worker adds only the resource pairing.
55
+ * - **Resource ↔ concurrency.** The pool's `max` defaults to the worker's
56
+ * `concurrency` (default `1`), so at most one resource exists per in-flight job and
57
+ * idle resources are reused across jobs.
58
+ * - **Acquire over the attempt signal.** Each job acquires using the attempt's
59
+ * `execution.signal`, so an `abort` / `timeout` while waiting for a resource rejects
60
+ * the acquire — the Queue then handles retry / rejection, and there is no token to
61
+ * release (the resource was never leased).
62
+ * - **Lifecycle (§10).** `enqueue` / `restore` / `start` / `stop` / `pause` / `resume` /
63
+ * `abort` / `clear` delegate to the queue; `count` / `active` / `paused` / `stopped`
64
+ * read it. `destroy` destroys the queue then tears the pool down, idempotently.
65
+ * - **Durability.** An optional `store` is passed straight through to the queue, so the
66
+ * worker's outstanding jobs persist; `restore` re-runs them (delegated to the queue).
67
+ * - **Observable (§13).** The owned {@link emitter} ({@link WorkerEventMap}) RE-EXPOSES the
68
+ * underlying queue's job lifecycle (`enqueue` / `start` / `retry` / `success` / `failure` /
69
+ * `abort` / `drain`) as the worker's OWN events — bridged from the inner queue's emitter at
70
+ * construction — so a consumer observes the worker without reaching through to internals.
71
+ * The bridge re-emits directly on the worker's own emitter; the worker emitter isolates a
72
+ * listener throw and routes it to its `error` handler (the `error` option), so a buggy
73
+ * worker observer can never corrupt the inner queue or pool — the bridge listener never
74
+ * throws, so the inner queue's own emit stays balanced. The pool's create / acquire /
75
+ * release events stay the pool's internal concern (a Worker manages its own resources);
76
+ * observe a `Pool` directly for those.
77
+ */
78
+ declare class Worker_2<TInput, TResource, TResult> implements WorkerInterface<TInput, TResult> {
79
+ #private;
80
+ constructor(options: WorkerOptions<TInput, TResource, TResult>);
81
+ get emitter(): EmitterInterface<WorkerEventMap<TResult>>;
82
+ get count(): number;
83
+ get active(): number;
84
+ get paused(): boolean;
85
+ get stopped(): boolean;
86
+ enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
87
+ restore(): Promise<void>;
88
+ start(): void;
89
+ stop(): void;
90
+ pause(): void;
91
+ resume(): void;
92
+ abort(reason?: unknown): void;
93
+ clear(): void;
94
+ destroy(): void;
95
+ }
96
+ export { Worker_2 as Worker }
97
+
98
+ /**
99
+ * The push observation surface of a {@link WorkerInterface} (AGENTS §13) — the job
100
+ * lifecycle a fire-and-forget observer subscribes to, surfacing the underlying queue's
101
+ * moments so a Worker consumer never reaches through to the internal `Queue`.
102
+ *
103
+ * @typeParam TResult - The value a job resolves (the `success` payload), mirroring the
104
+ * {@link WorkerInterface}'s own `TResult`.
105
+ *
106
+ * @remarks
107
+ * A Worker is a `Queue`⨉`Pool` facade (both from their own `@orkestrel` packages); this
108
+ * map RE-EXPOSES the queue lifecycle the worker surfaces (`enqueue` / `start` / `retry` /
109
+ * `success` / `failure` / `abort` / `drain`) as the worker's OWN events — wired from the
110
+ * underlying queue's emitter at construction, so a buggy observer is isolated exactly as
111
+ * on the queue (a throw routes to the worker emitter's `error` handler, AGENTS §13). The
112
+ * pool's create / acquire / release events stay the pool's internal concern (a Worker
113
+ * manages its own resources); a consumer who wants them observes a `Pool` directly.
114
+ * Declared as a `type` alias (§4.5).
115
+ */
116
+ export declare type WorkerEventMap<TResult> = {
117
+ /** A job was accepted — its id (delegated from the underlying queue's `enqueue`). */
118
+ readonly enqueue: readonly [id: string];
119
+ /** A job's attempt began running — its id. */
120
+ readonly start: readonly [id: string];
121
+ /** A failed job attempt is being retried — its id + the next (1-based) attempt index. */
122
+ readonly retry: readonly [id: string, attempt: number];
123
+ /** A job settled successfully — its id + the resolved result. */
124
+ readonly success: readonly [id: string, result: TResult];
125
+ /** A job settled with a terminal failure — its id + the error. */
126
+ readonly failure: readonly [id: string, error: unknown];
127
+ /** The worker was aborted — the cancel reason. */
128
+ readonly abort: readonly [reason: unknown];
129
+ /** The worker went idle — no pending jobs and none in flight. */
130
+ readonly drain: readonly [];
131
+ };
132
+
133
+ /** Runs one worker job with a leased pool resource. */
134
+ export declare type WorkerHandler<TInput, TResource, TResult> = (input: TInput, resource: TResource, execution: QueueExecution) => Promise<TResult> | TResult;
135
+
136
+ /**
137
+ * A resource-backed job worker — a Queue whose handler runs against a pooled resource.
138
+ *
139
+ * @remarks
140
+ * Exposes a typed {@link emitter} (AGENTS §13) carrying the job lifecycle
141
+ * ({@link WorkerEventMap}) — the underlying queue's moments re-exposed as the worker's own,
142
+ * so a consumer never reaches through to internals. Emitting is observation-only: a buggy
143
+ * observer is isolated exactly as on the queue (a throw routes to the emitter's `error`
144
+ * handler, the `error` option).
145
+ */
146
+ export declare interface WorkerInterface<TInput, TResult> {
147
+ readonly emitter: EmitterInterface<WorkerEventMap<TResult>>;
148
+ readonly count: number;
149
+ readonly active: number;
150
+ readonly paused: boolean;
151
+ readonly stopped: boolean;
152
+ enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
153
+ /** Re-enqueue outstanding entries loaded from the store; no-op without a store. */
154
+ restore(): Promise<void>;
155
+ start(): void;
156
+ stop(): void;
157
+ pause(): void;
158
+ resume(): void;
159
+ abort(reason?: unknown): void;
160
+ clear(): void;
161
+ destroy(): void;
162
+ }
163
+
164
+ /**
165
+ * Options for `createWorker`.
166
+ *
167
+ * @remarks
168
+ * - `handler` — runs each job against an acquired pool resource; rejecting triggers a
169
+ * retry while attempts remain (delegated to the underlying queue).
170
+ * - `pool` — the {@link PoolOptions} for the resource the handler runs against; its
171
+ * `max` defaults to `concurrency` so resources match the jobs in flight.
172
+ * - `concurrency` — the maximum jobs in flight at once; defaults to `1`. Floored at `1`.
173
+ * - `retries` — the default extra attempts per job on failure; defaults to `0`.
174
+ * - `timeout` — the default per-attempt deadline in milliseconds; defaults to none.
175
+ * - `store` — durable backing; outstanding entries survive a restart; call
176
+ * `restore()` to re-run them.
177
+ * - `on` — the reserved {@link EmitterHooks} key (§8): initial listeners for the worker's
178
+ * {@link WorkerEventMap} (the job lifecycle it surfaces from its underlying queue), wired
179
+ * at construction.
180
+ */
181
+ export declare interface WorkerOptions<TInput, TResource, TResult> {
182
+ readonly on?: EmitterHooks<WorkerEventMap<TResult>>;
183
+ /** The emitter's listener-error handler (AGENTS §13) — a listener throw routes here, not to a domain event. */
184
+ readonly error?: EmitterErrorHandler;
185
+ readonly handler: WorkerHandler<TInput, TResource, TResult>;
186
+ readonly pool: PoolOptions<TResource>;
187
+ readonly concurrency?: number;
188
+ readonly retries?: number;
189
+ readonly timeout?: number;
190
+ readonly store?: QueueStoreInterface<TInput>;
191
+ }
192
+
193
+ export { }
@@ -0,0 +1,167 @@
1
+ import { Emitter } from "@orkestrel/emitter";
2
+ import { Pool } from "@orkestrel/pool";
3
+ import { Queue } from "@orkestrel/queue";
4
+ //#region src/core/Worker.ts
5
+ /**
6
+ * A resource-backed job worker — a thin facade composing a `Queue` (`@orkestrel/queue`)
7
+ * with a `Pool` (`@orkestrel/pool`).
8
+ *
9
+ * @remarks
10
+ * - **Composition, not reimplementation.** The Worker owns a `Pool` (built from
11
+ * `options.pool`) and a `Queue` whose handler ACQUIRES a pooled resource, runs the
12
+ * user handler against it, and RELEASES it in a `finally`. All concurrency, retries,
13
+ * timeout, and lifecycle are the Queue's — the Worker adds only the resource pairing.
14
+ * - **Resource ↔ concurrency.** The pool's `max` defaults to the worker's
15
+ * `concurrency` (default `1`), so at most one resource exists per in-flight job and
16
+ * idle resources are reused across jobs.
17
+ * - **Acquire over the attempt signal.** Each job acquires using the attempt's
18
+ * `execution.signal`, so an `abort` / `timeout` while waiting for a resource rejects
19
+ * the acquire — the Queue then handles retry / rejection, and there is no token to
20
+ * release (the resource was never leased).
21
+ * - **Lifecycle (§10).** `enqueue` / `restore` / `start` / `stop` / `pause` / `resume` /
22
+ * `abort` / `clear` delegate to the queue; `count` / `active` / `paused` / `stopped`
23
+ * read it. `destroy` destroys the queue then tears the pool down, idempotently.
24
+ * - **Durability.** An optional `store` is passed straight through to the queue, so the
25
+ * worker's outstanding jobs persist; `restore` re-runs them (delegated to the queue).
26
+ * - **Observable (§13).** The owned {@link emitter} ({@link WorkerEventMap}) RE-EXPOSES the
27
+ * underlying queue's job lifecycle (`enqueue` / `start` / `retry` / `success` / `failure` /
28
+ * `abort` / `drain`) as the worker's OWN events — bridged from the inner queue's emitter at
29
+ * construction — so a consumer observes the worker without reaching through to internals.
30
+ * The bridge re-emits directly on the worker's own emitter; the worker emitter isolates a
31
+ * listener throw and routes it to its `error` handler (the `error` option), so a buggy
32
+ * worker observer can never corrupt the inner queue or pool — the bridge listener never
33
+ * throws, so the inner queue's own emit stays balanced. The pool's create / acquire /
34
+ * release events stay the pool's internal concern (a Worker manages its own resources);
35
+ * observe a `Pool` directly for those.
36
+ */
37
+ var Worker = class {
38
+ #queue;
39
+ #pool;
40
+ #emitter;
41
+ #destroyed = false;
42
+ constructor(options) {
43
+ const concurrency = Math.max(1, options.concurrency ?? 1);
44
+ this.#emitter = new Emitter({
45
+ on: options?.on,
46
+ error: options?.error
47
+ });
48
+ this.#pool = new Pool({
49
+ ...options.pool,
50
+ max: options.pool.max ?? concurrency
51
+ });
52
+ this.#queue = new Queue({
53
+ handler: async (input, execution) => {
54
+ const token = await this.#pool.acquire(execution.signal);
55
+ try {
56
+ return await options.handler(input, token.value, execution);
57
+ } finally {
58
+ token.release();
59
+ }
60
+ },
61
+ concurrency,
62
+ retries: options.retries,
63
+ timeout: options.timeout,
64
+ store: options.store
65
+ });
66
+ this.#bridge();
67
+ }
68
+ get emitter() {
69
+ return this.#emitter;
70
+ }
71
+ get count() {
72
+ return this.#queue.count;
73
+ }
74
+ get active() {
75
+ return this.#queue.active;
76
+ }
77
+ get paused() {
78
+ return this.#queue.paused;
79
+ }
80
+ get stopped() {
81
+ return this.#queue.stopped;
82
+ }
83
+ enqueue(input, options) {
84
+ return this.#queue.enqueue(input, options);
85
+ }
86
+ restore() {
87
+ return this.#queue.restore();
88
+ }
89
+ start() {
90
+ this.#queue.start();
91
+ }
92
+ stop() {
93
+ this.#queue.stop();
94
+ }
95
+ pause() {
96
+ this.#queue.pause();
97
+ }
98
+ resume() {
99
+ this.#queue.resume();
100
+ }
101
+ abort(reason) {
102
+ this.#queue.abort(reason);
103
+ }
104
+ clear() {
105
+ this.#queue.clear();
106
+ }
107
+ destroy() {
108
+ if (this.#destroyed) return;
109
+ this.#destroyed = true;
110
+ this.#queue.destroy();
111
+ this.#pool.destroy();
112
+ }
113
+ #bridge() {
114
+ const queue = this.#queue.emitter;
115
+ queue.on("enqueue", (id) => this.#emitter.emit("enqueue", id));
116
+ queue.on("start", (id) => this.#emitter.emit("start", id));
117
+ queue.on("retry", (id, attempt) => this.#emitter.emit("retry", id, attempt));
118
+ queue.on("success", (id, result) => this.#emitter.emit("success", id, result));
119
+ queue.on("failure", (id, error) => this.#emitter.emit("failure", id, error));
120
+ queue.on("abort", (reason) => this.#emitter.emit("abort", reason));
121
+ queue.on("drain", () => this.#emitter.emit("drain"));
122
+ }
123
+ };
124
+ //#endregion
125
+ //#region src/core/factories.ts
126
+ /**
127
+ * Create a resource-backed job worker — a `Queue` (`@orkestrel/queue`) marrying a `Pool`
128
+ * (`@orkestrel/pool`). Each enqueued input runs through the handler against an
129
+ * automatically acquired pooled resource (released when the job settles), with the
130
+ * queue's bounded concurrency, retries, and per-attempt timeout / abort.
131
+ *
132
+ * @remarks
133
+ * The pool's `max` defaults to `concurrency`, so resources match the jobs in flight and
134
+ * are reused across jobs. A handler that throws still releases its resource (the
135
+ * acquire/release pair brackets the call in a `finally`), so a later job reuses it. The
136
+ * lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
137
+ * delegates to the queue; `destroy` also tears the pool down. Observable (§13): a typed
138
+ * `emitter` surfaces the queue lifecycle (`enqueue` / `start` / `success` / `failure` / …).
139
+ *
140
+ * @typeParam TInput - The work input each job carries
141
+ * @typeParam TResource - The pooled resource each job runs against
142
+ * @typeParam TResult - The value the handler resolves for a job
143
+ * @param options - The `handler` and `pool` plus optional `concurrency` (default `1`),
144
+ * `retries` (default `0`), and a default per-attempt `timeout` in milliseconds
145
+ * @returns A working {@link WorkerInterface}
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * import { createWorker } from '@src/core'
150
+ *
151
+ * const worker = createWorker<Query, Connection, Rows>({
152
+ * pool: { create: () => connect(), destroy: (connection) => connection.close() },
153
+ * handler: (query, connection, { signal }) => connection.run(query, signal),
154
+ * concurrency: 4,
155
+ * retries: 1,
156
+ * })
157
+ *
158
+ * const rows = await worker.enqueue(query)
159
+ * ```
160
+ */
161
+ function createWorker(options) {
162
+ return new Worker(options);
163
+ }
164
+ //#endregion
165
+ export { Worker, createWorker };
166
+
167
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["#queue","#pool","#emitter","#bridge","#destroyed"],"sources":["../../../src/core/Worker.ts","../../../src/core/factories.ts"],"sourcesContent":["import type { EmitterInterface } from '@orkestrel/emitter'\nimport type { QueueEntryOptions } from '@orkestrel/queue'\nimport type { WorkerEventMap, WorkerInterface, WorkerOptions } from './types.js'\nimport { Emitter } from '@orkestrel/emitter'\nimport { Pool } from '@orkestrel/pool'\nimport { Queue } from '@orkestrel/queue'\n\n/**\n * A resource-backed job worker — a thin facade composing a `Queue` (`@orkestrel/queue`)\n * with a `Pool` (`@orkestrel/pool`).\n *\n * @remarks\n * - **Composition, not reimplementation.** The Worker owns a `Pool` (built from\n * `options.pool`) and a `Queue` whose handler ACQUIRES a pooled resource, runs the\n * user handler against it, and RELEASES it in a `finally`. All concurrency, retries,\n * timeout, and lifecycle are the Queue's — the Worker adds only the resource pairing.\n * - **Resource ↔ concurrency.** The pool's `max` defaults to the worker's\n * `concurrency` (default `1`), so at most one resource exists per in-flight job and\n * idle resources are reused across jobs.\n * - **Acquire over the attempt signal.** Each job acquires using the attempt's\n * `execution.signal`, so an `abort` / `timeout` while waiting for a resource rejects\n * the acquire — the Queue then handles retry / rejection, and there is no token to\n * release (the resource was never leased).\n * - **Lifecycle (§10).** `enqueue` / `restore` / `start` / `stop` / `pause` / `resume` /\n * `abort` / `clear` delegate to the queue; `count` / `active` / `paused` / `stopped`\n * read it. `destroy` destroys the queue then tears the pool down, idempotently.\n * - **Durability.** An optional `store` is passed straight through to the queue, so the\n * worker's outstanding jobs persist; `restore` re-runs them (delegated to the queue).\n * - **Observable (§13).** The owned {@link emitter} ({@link WorkerEventMap}) RE-EXPOSES the\n * underlying queue's job lifecycle (`enqueue` / `start` / `retry` / `success` / `failure` /\n * `abort` / `drain`) as the worker's OWN events — bridged from the inner queue's emitter at\n * construction — so a consumer observes the worker without reaching through to internals.\n * The bridge re-emits directly on the worker's own emitter; the worker emitter isolates a\n * listener throw and routes it to its `error` handler (the `error` option), so a buggy\n * worker observer can never corrupt the inner queue or pool — the bridge listener never\n * throws, so the inner queue's own emit stays balanced. The pool's create / acquire /\n * release events stay the pool's internal concern (a Worker manages its own resources);\n * observe a `Pool` directly for those.\n */\nexport class Worker<TInput, TResource, TResult> implements WorkerInterface<TInput, TResult> {\n\treadonly #queue: Queue<TInput, TResult>\n\treadonly #pool: Pool<TResource>\n\t// The PUSH observation surface (§13) — the worker's OWN emitter, fed by the queue→worker\n\t// bridge. The emitter isolates a worker observer's throw (routing it to the `error`\n\t// handler), so it never escapes into queue or pool.\n\treadonly #emitter: Emitter<WorkerEventMap<TResult>>\n\t#destroyed = false\n\n\tconstructor(options: WorkerOptions<TInput, TResource, TResult>) {\n\t\tconst concurrency = Math.max(1, options.concurrency ?? 1)\n\t\tthis.#emitter = new Emitter<WorkerEventMap<TResult>>({ on: options?.on, error: options?.error })\n\t\tthis.#pool = new Pool<TResource>({\n\t\t\t...options.pool,\n\t\t\tmax: options.pool.max ?? concurrency,\n\t\t})\n\t\tthis.#queue = new Queue<TInput, TResult>({\n\t\t\thandler: async (input, execution) => {\n\t\t\t\tconst token = await this.#pool.acquire(execution.signal)\n\t\t\t\ttry {\n\t\t\t\t\treturn await options.handler(input, token.value, execution)\n\t\t\t\t} finally {\n\t\t\t\t\ttoken.release()\n\t\t\t\t}\n\t\t\t},\n\t\t\tconcurrency,\n\t\t\tretries: options.retries,\n\t\t\ttimeout: options.timeout,\n\t\t\tstore: options.store,\n\t\t})\n\t\tthis.#bridge()\n\t}\n\n\tget emitter(): EmitterInterface<WorkerEventMap<TResult>> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#queue.count\n\t}\n\n\tget active(): number {\n\t\treturn this.#queue.active\n\t}\n\n\tget paused(): boolean {\n\t\treturn this.#queue.paused\n\t}\n\n\tget stopped(): boolean {\n\t\treturn this.#queue.stopped\n\t}\n\n\tenqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult> {\n\t\treturn this.#queue.enqueue(input, options)\n\t}\n\n\trestore(): Promise<void> {\n\t\treturn this.#queue.restore()\n\t}\n\n\tstart(): void {\n\t\tthis.#queue.start()\n\t}\n\n\tstop(): void {\n\t\tthis.#queue.stop()\n\t}\n\n\tpause(): void {\n\t\tthis.#queue.pause()\n\t}\n\n\tresume(): void {\n\t\tthis.#queue.resume()\n\t}\n\n\tabort(reason?: unknown): void {\n\t\tthis.#queue.abort(reason)\n\t}\n\n\tclear(): void {\n\t\tthis.#queue.clear()\n\t}\n\n\tdestroy(): void {\n\t\tif (this.#destroyed) return\n\t\tthis.#destroyed = true\n\t\tthis.#queue.destroy()\n\t\tvoid this.#pool.destroy()\n\t}\n\n\t// Bridge the inner queue's lifecycle onto the worker's OWN emitter, once at construction.\n\t// Each listener re-emits the queue event directly on the worker's emitter, which isolates a\n\t// worker observer's throw (routing it to the worker's `error` handler). Because the bridge\n\t// listener itself never throws, the queue's own `#emitter.emit` — which invoked this\n\t// listener — sees no throw, so the inner queue's engine stays balanced regardless of what a\n\t// worker observer does. The events are already post-transition (they fire from the queue's\n\t// own post-settle / post-wake emits), so this stays observation.\n\t#bridge(): void {\n\t\tconst queue = this.#queue.emitter\n\t\tqueue.on('enqueue', (id) => this.#emitter.emit('enqueue', id))\n\t\tqueue.on('start', (id) => this.#emitter.emit('start', id))\n\t\tqueue.on('retry', (id, attempt) => this.#emitter.emit('retry', id, attempt))\n\t\tqueue.on('success', (id, result) => this.#emitter.emit('success', id, result))\n\t\tqueue.on('failure', (id, error) => this.#emitter.emit('failure', id, error))\n\t\tqueue.on('abort', (reason) => this.#emitter.emit('abort', reason))\n\t\tqueue.on('drain', () => this.#emitter.emit('drain'))\n\t}\n}\n","import type { WorkerInterface, WorkerOptions } from './types.js'\nimport { Worker } from './Worker.js'\n\n/**\n * Create a resource-backed job worker — a `Queue` (`@orkestrel/queue`) marrying a `Pool`\n * (`@orkestrel/pool`). Each enqueued input runs through the handler against an\n * automatically acquired pooled resource (released when the job settles), with the\n * queue's bounded concurrency, retries, and per-attempt timeout / abort.\n *\n * @remarks\n * The pool's `max` defaults to `concurrency`, so resources match the jobs in flight and\n * are reused across jobs. A handler that throws still releases its resource (the\n * acquire/release pair brackets the call in a `finally`), so a later job reuses it. The\n * lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)\n * delegates to the queue; `destroy` also tears the pool down. Observable (§13): a typed\n * `emitter` surfaces the queue lifecycle (`enqueue` / `start` / `success` / `failure` / …).\n *\n * @typeParam TInput - The work input each job carries\n * @typeParam TResource - The pooled resource each job runs against\n * @typeParam TResult - The value the handler resolves for a job\n * @param options - The `handler` and `pool` plus optional `concurrency` (default `1`),\n * `retries` (default `0`), and a default per-attempt `timeout` in milliseconds\n * @returns A working {@link WorkerInterface}\n *\n * @example\n * ```ts\n * import { createWorker } from '@src/core'\n *\n * const worker = createWorker<Query, Connection, Rows>({\n * \tpool: { create: () => connect(), destroy: (connection) => connection.close() },\n * \thandler: (query, connection, { signal }) => connection.run(query, signal),\n * \tconcurrency: 4,\n * \tretries: 1,\n * })\n *\n * const rows = await worker.enqueue(query)\n * ```\n */\nexport function createWorker<TInput, TResource, TResult>(\n\toptions: WorkerOptions<TInput, TResource, TResult>,\n): WorkerInterface<TInput, TResult> {\n\treturn new Worker(options)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,IAAa,SAAb,MAA4F;CAC3F;CACA;CAIA;CACA,aAAa;CAEb,YAAY,SAAoD;EAC/D,MAAM,cAAc,KAAK,IAAI,GAAG,QAAQ,eAAe,CAAC;EACxD,KAAKE,WAAW,IAAI,QAAiC;GAAE,IAAI,SAAS;GAAI,OAAO,SAAS;EAAM,CAAC;EAC/F,KAAKD,QAAQ,IAAI,KAAgB;GAChC,GAAG,QAAQ;GACX,KAAK,QAAQ,KAAK,OAAO;EAC1B,CAAC;EACD,KAAKD,SAAS,IAAI,MAAuB;GACxC,SAAS,OAAO,OAAO,cAAc;IACpC,MAAM,QAAQ,MAAM,KAAKC,MAAM,QAAQ,UAAU,MAAM;IACvD,IAAI;KACH,OAAO,MAAM,QAAQ,QAAQ,OAAO,MAAM,OAAO,SAAS;IAC3D,UAAU;KACT,MAAM,QAAQ;IACf;GACD;GACA;GACA,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB,OAAO,QAAQ;EAChB,CAAC;EACD,KAAKE,QAAQ;CACd;CAEA,IAAI,UAAqD;EACxD,OAAO,KAAKD;CACb;CAEA,IAAI,QAAgB;EACnB,OAAO,KAAKF,OAAO;CACpB;CAEA,IAAI,SAAiB;EACpB,OAAO,KAAKA,OAAO;CACpB;CAEA,IAAI,SAAkB;EACrB,OAAO,KAAKA,OAAO;CACpB;CAEA,IAAI,UAAmB;EACtB,OAAO,KAAKA,OAAO;CACpB;CAEA,QAAQ,OAAe,SAA+C;EACrE,OAAO,KAAKA,OAAO,QAAQ,OAAO,OAAO;CAC1C;CAEA,UAAyB;EACxB,OAAO,KAAKA,OAAO,QAAQ;CAC5B;CAEA,QAAc;EACb,KAAKA,OAAO,MAAM;CACnB;CAEA,OAAa;EACZ,KAAKA,OAAO,KAAK;CAClB;CAEA,QAAc;EACb,KAAKA,OAAO,MAAM;CACnB;CAEA,SAAe;EACd,KAAKA,OAAO,OAAO;CACpB;CAEA,MAAM,QAAwB;EAC7B,KAAKA,OAAO,MAAM,MAAM;CACzB;CAEA,QAAc;EACb,KAAKA,OAAO,MAAM;CACnB;CAEA,UAAgB;EACf,IAAI,KAAKI,YAAY;EACrB,KAAKA,aAAa;EAClB,KAAKJ,OAAO,QAAQ;EACpB,KAAUC,MAAM,QAAQ;CACzB;CASA,UAAgB;EACf,MAAM,QAAQ,KAAKD,OAAO;EAC1B,MAAM,GAAG,YAAY,OAAO,KAAKE,SAAS,KAAK,WAAW,EAAE,CAAC;EAC7D,MAAM,GAAG,UAAU,OAAO,KAAKA,SAAS,KAAK,SAAS,EAAE,CAAC;EACzD,MAAM,GAAG,UAAU,IAAI,YAAY,KAAKA,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC;EAC3E,MAAM,GAAG,YAAY,IAAI,WAAW,KAAKA,SAAS,KAAK,WAAW,IAAI,MAAM,CAAC;EAC7E,MAAM,GAAG,YAAY,IAAI,UAAU,KAAKA,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC;EAC3E,MAAM,GAAG,UAAU,WAAW,KAAKA,SAAS,KAAK,SAAS,MAAM,CAAC;EACjE,MAAM,GAAG,eAAe,KAAKA,SAAS,KAAK,OAAO,CAAC;CACpD;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9GA,SAAgB,aACf,SACmC;CACnC,OAAO,IAAI,OAAO,OAAO;AAC1B"}