mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-06-06 19:35:44 +02:00
server for rowboatx
This commit is contained in:
parent
ae877e70ae
commit
9ad6331fbc
38 changed files with 2223 additions and 1088 deletions
44
apps/cli/src/application/lib/message-queue.ts
Normal file
44
apps/cli/src/application/lib/message-queue.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import z from "zod";
|
||||
import { IMonotonicallyIncreasingIdGenerator } from "./id-gen.js";
|
||||
|
||||
const EnqueuedMessage = z.object({
|
||||
messageId: z.string(),
|
||||
message: z.string(),
|
||||
});
|
||||
|
||||
export interface IMessageQueue {
|
||||
enqueue(runId: string, message: string): Promise<string>;
|
||||
dequeue(runId: string): Promise<z.infer<typeof EnqueuedMessage> | null>;
|
||||
}
|
||||
|
||||
export class InMemoryMessageQueue implements IMessageQueue {
|
||||
private store: Record<string, z.infer<typeof EnqueuedMessage>[]> = {};
|
||||
private idGenerator: IMonotonicallyIncreasingIdGenerator;
|
||||
|
||||
constructor({
|
||||
idGenerator,
|
||||
}: {
|
||||
idGenerator: IMonotonicallyIncreasingIdGenerator;
|
||||
}) {
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
async enqueue(runId: string, message: string): Promise<string> {
|
||||
if (!this.store[runId]) {
|
||||
this.store[runId] = [];
|
||||
}
|
||||
const id = await this.idGenerator.next();
|
||||
this.store[runId].push({
|
||||
messageId: id,
|
||||
message,
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
async dequeue(runId: string): Promise<z.infer<typeof EnqueuedMessage> | null> {
|
||||
if (!this.store[runId]) {
|
||||
return null;
|
||||
}
|
||||
return this.store[runId].shift() ?? null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue