Skip to content

Commit

Permalink
refactor: remove ignore as onTimeout option (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy authored Feb 1, 2025
1 parent 1afed16 commit 3735e16
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/convenience/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token" as const;
// const adapters = { ...nativeAdapters, callback: callbackAdapter };

export interface WebhookOptions {
/** An optional strategy to handle timeouts (default: 'throw') */
onTimeout?: "throw" | "ignore" | ((...args: any[]) => unknown);
/** An optional callback to handle timeouts. If undefined, it will throw an error. */
onTimeout?: (...args: any[]) => unknown | Promise<unknown>;
/** An optional number of timeout milliseconds (default: 10_000) */
timeoutMilliseconds?: number;
/** An optional string to compare to X-Telegram-Bot-Api-Secret-Token */
Expand Down Expand Up @@ -170,7 +170,7 @@ function webhookCallback<
options?: WebhookOptions,
): (...args: Parameters<T>) => Promise<ReturnType<T>> {
const {
onTimeout = "throw",
onTimeout,
timeoutMilliseconds = 10_000,
secretToken,
} = options ?? {};
Expand Down Expand Up @@ -237,10 +237,8 @@ function webhookCallback<
};
await timeoutIfNecessary(
bot.handleUpdate(updateData, webhookReplyEnvelope),
typeof onTimeout === "function"
? () => onTimeout(...args)
: onTimeout,
timeoutMilliseconds,
onTimeout !== undefined ? () => onTimeout(...args) : undefined,
);

if (!usedWebhookReply) return ok(...args);
Expand All @@ -252,27 +250,30 @@ function webhookCallback<

function timeoutIfNecessary(
task: Promise<void>,
onTimeout: "throw" | "ignore" | (() => unknown),
timeout: number,
onTimeout?: () => unknown | Promise<unknown>,
): Promise<void> {
if (timeout === Infinity) return task;
return new Promise((resolve, reject) => {
const handle = setTimeout(() => {
debugErr(`Request timed out after ${timeout} ms`);
if (onTimeout === "throw") {
reject(new Error(`Request timed out after ${timeout} ms`));
} else {
if (typeof onTimeout === "function") onTimeout();
resolve();
}
const now = Date.now();
task.finally(() => {
const diff = Date.now() - now;
debugErr(`Request completed ${diff} ms after timeout!`);
});
}, timeout);
task.then(resolve)
.catch(reject)
.finally(() => clearTimeout(handle));
});
const { promise, resolve, reject } = Promise.withResolvers<void>();

const handle = setTimeout(async () => {
debugErr(`Request timed out after ${timeout} ms`);
if (onTimeout !== undefined) {
await onTimeout();
resolve();
} else {
reject(new Error(`Request timed out after ${timeout} ms`));
}
const now = Date.now();
task.finally(() => {
const diff = Date.now() - now;
debugErr(`Request completed ${diff} ms after timeout!`);
});
}, timeout);

task.then(resolve)
.catch(reject)
.finally(() => clearTimeout(handle));

return promise;
}

0 comments on commit 3735e16

Please sign in to comment.