1import * as Effect from "effect/Effect";
2import * as Layer from "effect/Layer";
3import { makeQueueBinding, makeQueueHelpers } from "./QueueBinding.ts";
4import type { SendMessage, SendOptions } from "./QueueTypes.ts";
5import { WriteQueue, type WriteQueueClient } from "./WriteQueue.ts";
6
7/**
8 * Implementation of the {@link WriteQueue} service that uses a native Worker
9 * queue binding.
10 */
11export const WriteQueueBinding = Layer.effect(
12 WriteQueue,
13 Effect.suspend(() => makeQueueBinding({ makeClient: makeWriteQueueClient })),
14);
15
16/** Build the producer client over a native Worker queue binding. */
17export const makeWriteQueueClient = ({
18 raw,
19 use,
20}: ReturnType<typeof makeQueueHelpers>): WriteQueueClient => ({
21 raw,
22 send: (body: unknown, options?: SendOptions) =>
23 use((q) => q.send(body, options)),
24 sendBatch: (messages: ReadonlyArray<SendMessage>) =>
25 use((q) =>
26 q.sendBatch(
27 messages.map((m) => ({
28 body: m.body,
29 ...(m.contentType ? { contentType: m.contentType } : {}),
30 })),
31 ),
32 ),
33});