Skip to content

Commit

Permalink
Add method dropPendingUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Apr 11, 2024
1 parent 5f975e0 commit 8a992dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
17 changes: 17 additions & 0 deletions client_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,21 @@ export class ClientManager {
this.#activeWebhookLoops.delete(client);
}
}

async dropPendingUpdates(id: string) {
const client = await this.getClient(id);
if (!client) {
return;
}
const kv = this.#kvMap.get(client);
if (!kv) {
return;
}
for await (
const update of kv.list({ prefix: [ClientManager.#PENDING_UPDATES] })
) {
await kv.delete(update.key);
}
this.#updates.set(client, []);
}
}
14 changes: 13 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ async function handleRequest(id: string, method: string, params: any[]) {
});
}
return await handleDeleteWebhook(worker, id);

case "dropPendingUpdates":
if (params.length != 0) {
return Response.json("No arguments were expected.", {
status: 400,
headers: { "x-error-type": "input" },
});
}
return await handleDropPendingUpdates(worker, id);
default:
return Response.json("Invalid method", {
status: 400,
Expand Down Expand Up @@ -198,3 +205,8 @@ async function handleDeleteWebhook(worker: number, id: string) {
const result = await workers.call(worker, "deleteWebhook", id);
return Response.json(...result);
}

async function handleDropPendingUpdates(worker: number, id: string) {
const result = await workers.call(worker, "dropPendingUpdates", id);
return Response.json(...result);
}
8 changes: 8 additions & 0 deletions worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const handlers = {
deleteWebhook,
startWebhookLoop,
unload,
dropPendingUpdates,
};
export type Handler = typeof handlers;

Expand Down Expand Up @@ -233,3 +234,10 @@ async function startWebhookLoop(id: string) {
function unload() {
dispatchEvent(new Event("unload"));
}

async function dropPendingUpdates(
id: string,
): Promise<Parameters<typeof Response["json"]>> {
await clientManager.dropPendingUpdates(id);
return [null];
}

0 comments on commit 8a992dc

Please sign in to comment.