summaryrefslogtreecommitdiff
path: root/ext/cron/01_cron.ts
diff options
context:
space:
mode:
authorIgor Zinkovsky <igor@deno.com>2023-11-16 14:19:00 -0800
committerGitHub <noreply@github.com>2023-11-16 14:19:00 -0800
commitb572abfcb3fadbfdd3ce671a27463d67bcb77534 (patch)
treea3b783218079b01b16a331f7522d3a3935603520 /ext/cron/01_cron.ts
parent6b42cecc064d01d87aae978ecd7eb372bfe9a34e (diff)
feat(ext/cron) modify Deno.cron API to make handler arg last (#21225)
This PR changes the `Deno.cron` API: * Marks the existing function as deprecated * Introduces 2 new overloads, where the handler arg is always last: ```ts Deno.cron( name: string, schedule: string, handler: () => Promise<void> | void, ) Deno.cron( name: string, schedule: string, options?: { backoffSchedule?: number[]; signal?: AbortSignal }, handler: () => Promise<void> | void, ) ``` This PR also fixes a bug, when other crons continue execution after one of the crons was closed using `signal`.
Diffstat (limited to 'ext/cron/01_cron.ts')
-rw-r--r--ext/cron/01_cron.ts23
1 files changed, 20 insertions, 3 deletions
diff --git a/ext/cron/01_cron.ts b/ext/cron/01_cron.ts
index a615ae34b..30343905c 100644
--- a/ext/cron/01_cron.ts
+++ b/ext/cron/01_cron.ts
@@ -6,8 +6,12 @@ const core = Deno.core;
function cron(
name: string,
schedule: string,
- handler: () => Promise<void> | void,
- options?: { backoffSchedule?: number[]; signal?: AbortSignal },
+ handlerOrOptions1:
+ | (() => Promise<void> | void)
+ | ({ backoffSchedule?: number[]; signal?: AbortSignal }),
+ handlerOrOptions2?:
+ | (() => Promise<void> | void)
+ | ({ backoffSchedule?: number[]; signal?: AbortSignal }),
) {
if (name === undefined) {
throw new TypeError("Deno.cron requires a unique name");
@@ -15,7 +19,20 @@ function cron(
if (schedule === undefined) {
throw new TypeError("Deno.cron requires a valid schedule");
}
- if (handler === undefined) {
+
+ let handler: () => Promise<void> | void;
+ let options: { backoffSchedule?: number[]; signal?: AbortSignal } | undefined;
+
+ if (typeof handlerOrOptions1 === "function") {
+ handler = handlerOrOptions1;
+ if (typeof handlerOrOptions2 === "function") {
+ throw new TypeError("options must be an object");
+ }
+ options = handlerOrOptions2;
+ } else if (typeof handlerOrOptions2 === "function") {
+ handler = handlerOrOptions2;
+ options = handlerOrOptions1;
+ } else {
throw new TypeError("Deno.cron requires a handler");
}