Skip to content

Commit 3735e16

Browse files
refactor: remove ignore as onTimeout option (#755)
1 parent 1afed16 commit 3735e16

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/convenience/webhook.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token" as const;
2626
// const adapters = { ...nativeAdapters, callback: callbackAdapter };
2727

2828
export interface WebhookOptions {
29-
/** An optional strategy to handle timeouts (default: 'throw') */
30-
onTimeout?: "throw" | "ignore" | ((...args: any[]) => unknown);
29+
/** An optional callback to handle timeouts. If undefined, it will throw an error. */
30+
onTimeout?: (...args: any[]) => unknown | Promise<unknown>;
3131
/** An optional number of timeout milliseconds (default: 10_000) */
3232
timeoutMilliseconds?: number;
3333
/** An optional string to compare to X-Telegram-Bot-Api-Secret-Token */
@@ -170,7 +170,7 @@ function webhookCallback<
170170
options?: WebhookOptions,
171171
): (...args: Parameters<T>) => Promise<ReturnType<T>> {
172172
const {
173-
onTimeout = "throw",
173+
onTimeout,
174174
timeoutMilliseconds = 10_000,
175175
secretToken,
176176
} = options ?? {};
@@ -237,10 +237,8 @@ function webhookCallback<
237237
};
238238
await timeoutIfNecessary(
239239
bot.handleUpdate(updateData, webhookReplyEnvelope),
240-
typeof onTimeout === "function"
241-
? () => onTimeout(...args)
242-
: onTimeout,
243240
timeoutMilliseconds,
241+
onTimeout !== undefined ? () => onTimeout(...args) : undefined,
244242
);
245243

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

253251
function timeoutIfNecessary(
254252
task: Promise<void>,
255-
onTimeout: "throw" | "ignore" | (() => unknown),
256253
timeout: number,
254+
onTimeout?: () => unknown | Promise<unknown>,
257255
): Promise<void> {
258256
if (timeout === Infinity) return task;
259-
return new Promise((resolve, reject) => {
260-
const handle = setTimeout(() => {
261-
debugErr(`Request timed out after ${timeout} ms`);
262-
if (onTimeout === "throw") {
263-
reject(new Error(`Request timed out after ${timeout} ms`));
264-
} else {
265-
if (typeof onTimeout === "function") onTimeout();
266-
resolve();
267-
}
268-
const now = Date.now();
269-
task.finally(() => {
270-
const diff = Date.now() - now;
271-
debugErr(`Request completed ${diff} ms after timeout!`);
272-
});
273-
}, timeout);
274-
task.then(resolve)
275-
.catch(reject)
276-
.finally(() => clearTimeout(handle));
277-
});
257+
const { promise, resolve, reject } = Promise.withResolvers<void>();
258+
259+
const handle = setTimeout(async () => {
260+
debugErr(`Request timed out after ${timeout} ms`);
261+
if (onTimeout !== undefined) {
262+
await onTimeout();
263+
resolve();
264+
} else {
265+
reject(new Error(`Request timed out after ${timeout} ms`));
266+
}
267+
const now = Date.now();
268+
task.finally(() => {
269+
const diff = Date.now() - now;
270+
debugErr(`Request completed ${diff} ms after timeout!`);
271+
});
272+
}, timeout);
273+
274+
task.then(resolve)
275+
.catch(reject)
276+
.finally(() => clearTimeout(handle));
277+
278+
return promise;
278279
}

0 commit comments

Comments
 (0)