@stacksjs/scheduler 0.58.51 → 0.58.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ import type { SpawnOptions } from 'node:child_process'
2
+ import type { DateTime } from 'luxon'
3
+ import type { CONSTRAINTS, TIME_UNITS_MAP } from '../constants'
4
+ import type { CronJob } from '../job'
5
+ import type { IntRange } from './utils'
6
+
7
+ interface BaseCronJobParams<
8
+ OC extends CronOnCompleteCommand | null = null,
9
+ C = null,
10
+ > {
11
+ cronTime: string | Date | DateTime
12
+ onTick: CronCommand<C, WithOnComplete<OC>>
13
+ onComplete?: OC
14
+ start?: boolean | null
15
+ context?: C
16
+ runOnInit?: boolean | null
17
+ unrefTimeout?: boolean | null
18
+ }
19
+
20
+ export type CronJobParams<
21
+ OC extends CronOnCompleteCommand | null = null,
22
+ C = null,
23
+ > = BaseCronJobParams<OC, C> &
24
+ (
25
+ | {
26
+ timeZone?: string | null
27
+ utcOffset?: never
28
+ }
29
+ | {
30
+ timeZone?: never
31
+ utcOffset?: number | null
32
+ }
33
+ )
34
+
35
+ export type CronContext<C> = C extends null ? CronJob : NonNullable<C>
36
+
37
+ export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (
38
+ this: CronContext<C>,
39
+ onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never
40
+ ) => void | Promise<void>
41
+
42
+ export type CronOnCompleteCallback = () => void | Promise<void>
43
+
44
+ export type CronSystemCommand =
45
+ | string
46
+ | {
47
+ command: string
48
+ args?: readonly string[] | null
49
+ options?: SpawnOptions | null
50
+ }
51
+
52
+ export type CronCommand<C, WithOnCompleteBool extends boolean = false> =
53
+ | CronCallback<C, WithOnCompleteBool>
54
+ | CronSystemCommand
55
+
56
+ export type CronOnCompleteCommand = CronOnCompleteCallback | CronSystemCommand
57
+
58
+ export type WithOnComplete<OC> = OC extends null ? false : true
59
+
60
+ export type TimeUnit = (typeof TIME_UNITS_MAP)[keyof typeof TIME_UNITS_MAP]
61
+
62
+ export type TimeUnitField<T extends TimeUnit> = Partial<
63
+ Record<Ranges[T], boolean>
64
+ >
65
+
66
+ export interface Ranges {
67
+ second: SecondRange
68
+ minute: MinuteRange
69
+ hour: HourRange
70
+ dayOfMonth: DayOfMonthRange
71
+ month: MonthRange
72
+ dayOfWeek: DayOfWeekRange
73
+ }
74
+
75
+ export type SecondRange = IntRange<
76
+ (typeof CONSTRAINTS)['second'][0],
77
+ (typeof CONSTRAINTS)['second'][1]
78
+ >
79
+ export type MinuteRange = IntRange<
80
+ (typeof CONSTRAINTS)['minute'][0],
81
+ (typeof CONSTRAINTS)['minute'][1]
82
+ >
83
+ export type HourRange = IntRange<
84
+ (typeof CONSTRAINTS)['hour'][0],
85
+ (typeof CONSTRAINTS)['hour'][1]
86
+ >
87
+ export type DayOfMonthRange = IntRange<
88
+ (typeof CONSTRAINTS)['dayOfMonth'][0],
89
+ (typeof CONSTRAINTS)['dayOfMonth'][1]
90
+ >
91
+ export type MonthRange = IntRange<
92
+ (typeof CONSTRAINTS)['month'][0],
93
+ (typeof CONSTRAINTS)['month'][1]
94
+ >
95
+ export type DayOfWeekRange = IntRange<
96
+ (typeof CONSTRAINTS)['dayOfWeek'][0],
97
+ (typeof CONSTRAINTS)['dayOfWeek'][1]
98
+ >
@@ -0,0 +1,14 @@
1
+ export type IntRange<F extends number, T extends number> = Exclude<
2
+ Enumerate<T>,
3
+ Enumerate<F, false>
4
+ >
5
+
6
+ type Enumerate<
7
+ N extends number,
8
+ WithTail extends boolean = true,
9
+ Acc extends number[] = [],
10
+ > = Acc['length'] extends N
11
+ ? WithTail extends true
12
+ ? [...Acc, Acc['length']][number]
13
+ : Acc[number]
14
+ : Enumerate<N, WithTail, [...Acc, Acc['length']]>
package/src/utils.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { Ranges } from './types/cron'
2
+ import { ExclusiveParametersError } from './errors'
3
+
4
+ export function getRecordKeys<K extends Ranges[keyof Ranges]>(record: Partial<Record<K, boolean>>) {
5
+ return Object.keys(record) as unknown as (keyof typeof record)[]
6
+ }
7
+
8
+ export function getTimeZoneAndOffset(timeZone?: string | null, utcOffset?: number | null) {
9
+ if (timeZone != null && utcOffset != null)
10
+ throw new ExclusiveParametersError('timeZone', 'utcOffset')
11
+
12
+ if (timeZone != null)
13
+ return { timeZone, utcOffset: null }
14
+
15
+ if (utcOffset != null)
16
+ return { timeZone: null, utcOffset }
17
+
18
+ return { timeZone: null, utcOffset: null }
19
+ }