| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { makeQueueBinding, makeQueueHelpers } from "./QueueBinding.ts"; |
| 4 | import type { SendMessage, SendOptions } from "./QueueTypes.ts"; |
| 5 | import { WriteQueue, type WriteQueueClient } from "./WriteQueue.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Implementation of the {@link WriteQueue} service that uses a native Worker |
| 9 | * queue binding. |
| 10 | */ |
| 11 | export 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. */ |
| 17 | export 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 | }); |