const router = HttpRouter.empty.pipe(
HttpRouter.post("/submit", postSubmitHandler.pipe(Effect.provide(PostSubmitQueue.Default))),
);
const postSubmitHandler = Effect.gen(function* () {
const postSubmitQueue = yield* PostSubmitQueue
const params = yield* ParsedSearchParams;
const txStringParam = params["id"];
postSubmitQueue.put(txStringParam)
const fiber = yield* postSubmitQueue.retrieve()
return yield* Fiber.join(fiber)
}).pipe(
Effect.catchAll((e) =>
Effect.gen(function* () {
return yield* HttpServerResponse.json(
{ error: `Something went wrong: ${e}` },
{ status: 400 },
);
}),
),
);
export class PostSubmitQueue extends Effect.Service<PostSubmitQueue>()(
"PostSubmitQueue",
{
effect: Effect.gen(function* () {
const localQueue = yield* Queue.unbounded<
Effect.Effect<
RuntimeFiber<HttpServerResponse.HttpServerResponse, HttpBodyError | Error>,
never,
SqlClient
>
>();
const put = (data: string | string[]) =>
localQueue.offer(Effect.fork(postSubmitHelper(data)));
const retrieve = () => Effect.gen(function* () {
const fiberEffect = yield* localQueue.take
const fiber = yield* fiberEffect
return fiber
})
return {
put,
retrieve,
} as const;
}),
},
) {}
const postSubmitHelper = (txStringParam: string | string[]) => Effect.gen(function* () {
// Attemt to submit to the database and return the `HttpResponse`
})
const router = HttpRouter.empty.pipe(
HttpRouter.post("/submit", postSubmitHandler.pipe(Effect.provide(PostSubmitQueue.Default))),
);
const postSubmitHandler = Effect.gen(function* () {
const postSubmitQueue = yield* PostSubmitQueue
const params = yield* ParsedSearchParams;
const txStringParam = params["id"];
postSubmitQueue.put(txStringParam)
const fiber = yield* postSubmitQueue.retrieve()
return yield* Fiber.join(fiber)
}).pipe(
Effect.catchAll((e) =>
Effect.gen(function* () {
return yield* HttpServerResponse.json(
{ error: `Something went wrong: ${e}` },
{ status: 400 },
);
}),
),
);
export class PostSubmitQueue extends Effect.Service<PostSubmitQueue>()(
"PostSubmitQueue",
{
effect: Effect.gen(function* () {
const localQueue = yield* Queue.unbounded<
Effect.Effect<
RuntimeFiber<HttpServerResponse.HttpServerResponse, HttpBodyError | Error>,
never,
SqlClient
>
>();
const put = (data: string | string[]) =>
localQueue.offer(Effect.fork(postSubmitHelper(data)));
const retrieve = () => Effect.gen(function* () {
const fiberEffect = yield* localQueue.take
const fiber = yield* fiberEffect
return fiber
})
return {
put,
retrieve,
} as const;
}),
},
) {}
const postSubmitHelper = (txStringParam: string | string[]) => Effect.gen(function* () {
// Attemt to submit to the database and return the `HttpResponse`
})