Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/webhooks/webhook_preview/lib/sse-store.ts
1085 views
1
import { WebhookData } from "@/types/webhook";
2
3
type Client = {
4
id: string;
5
res: Response;
6
};
7
8
const clients: Map<string, Client[]> = new Map();
9
10
export function addClient(uuid: string, client: Client) {
11
const current = clients.get(uuid) || [];
12
clients.set(uuid, [...current, client]);
13
}
14
15
export function removeClient(uuid: string, clientId: string) {
16
const current = clients.get(uuid) || [];
17
clients.set(
18
uuid,
19
current.filter((c) => c.id !== clientId),
20
);
21
}
22
23
export function sendEvent(uuid: string, data: WebhookData) {
24
const current = clients.get(uuid) || [];
25
current.forEach((client) => {
26
try {
27
client.res.write?.(`data: ${JSON.stringify(data)}\n\n`);
28
} catch (err) {
29
console.error("Erro ao enviar SSE:", err);
30
}
31
});
32
}
33
34