Skip to content

Commit

Permalink
Send SAMMI webhooks in a queue
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox committed Dec 4, 2023
1 parent 24d303c commit e111089
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions examples/sammi_sail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Sail } from "../Sail.ts";
import { SohClient } from "../SohClient.ts";
import { Hook } from "../types.ts";

let port = 43384;
if (Deno.env.has("PORT")) {
Expand Down Expand Up @@ -28,17 +29,26 @@ const sammiUrl = Deno.env.has("SAMMI_WEBHOOK_URL")
const sail = new Sail({ port, debug: true });
let sohClient: SohClient | undefined;

const hookQueue: Hook[] = [];

sail.on("clientConnected", (client) => {
sohClient = client;

client.on("disconnected", () => {
sohClient = undefined;
});

client.on("anyHook", async (event) => {
const { type, ...rest } = event;
client.on("anyHook", (event) => {
hookQueue.push(event);
});
});

(async function processHookQueue() {
try {
if (hookQueue.length > 0) {
const event = hookQueue.shift()!;
const { type, ...rest } = event;

try {
await fetch(sammiUrl, {
method: "POST",
headers: {
Expand All @@ -49,11 +59,13 @@ sail.on("clientConnected", (client) => {
...rest,
}),
});
} catch (error) {
console.error("Error sending webhook to SAMMI", error);
}
});
});
} catch (error) {
console.error("There was an error sending a hook to Sammi", error);
} finally {
setTimeout(processHookQueue, 0);
}
})();

async function handler(request: Request): Promise<Response> {
if (!request.body) {
Expand Down

0 comments on commit e111089

Please sign in to comment.