@@ -53,13 +53,14 @@ const maxDelay: number = 30 * 1000;
53
53
/**
54
54
* An array containing all named cron jobs.
55
55
*/
56
- const scheduledJobs : Cron [ ] = [ ] ;
56
+ // deno-lint-ignore no-explicit-any
57
+ const scheduledJobs : Cron < any > [ ] = [ ] ;
57
58
58
59
/**
59
60
* Encapsulate all internal states in the Cron instance.
60
61
* Duplicate all options that can change to internal states, for example maxRuns and paused.
61
62
*/
62
- type CronState = {
63
+ type CronState < T = undefined > = {
63
64
kill : boolean ;
64
65
blocking : boolean ;
65
66
/**
@@ -68,15 +69,15 @@ type CronState = {
68
69
* Stored to use as the actual previous run, even while a new trigger
69
70
* is started. Used by the public funtion `.previousRun()`
70
71
*/
71
- previousRun : CronDate | undefined ;
72
+ previousRun : CronDate < T > | undefined ;
72
73
/**
73
74
* Start time of current trigger, this is updated just before triggering
74
75
*
75
76
* This is used internally as "previous run", as we mostly want to know
76
77
* when the previous run _started_
77
78
*/
78
- currentRun : CronDate | undefined ;
79
- once : CronDate | undefined ;
79
+ currentRun : CronDate < T > | undefined ;
80
+ once : CronDate < T > | undefined ;
80
81
//@ts -ignore Cross Runtime
81
82
currentTimeout : NodeJS . Timer | number | undefined ;
82
83
maxRuns : number | undefined ;
@@ -92,9 +93,9 @@ type CronState = {
92
93
*
93
94
* @returns void or Promise<void> for async callbacks
94
95
*/
95
- export type CronCallback = (
96
- self : InstanceType < typeof Cron > ,
97
- context : unknown ,
96
+ export type CronCallback < T = undefined > = (
97
+ self : InstanceType < typeof Cron < T > > ,
98
+ context : T ,
98
99
) => void | Promise < void > ;
99
100
100
101
/**
@@ -105,41 +106,42 @@ export type CronCallback = (
105
106
* @param [fnOrOptions1] - Options or function to be run each iteration of pattern
106
107
* @param [fnOrOptions2] - Options or function to be run each iteration of pattern
107
108
*/
108
- class Cron {
109
+ class Cron < T = undefined > {
109
110
name : string | undefined ;
110
- options : CronOptions ;
111
- private _states : CronState ;
112
- private fn ?: CronCallback ;
111
+ options : CronOptions < T > ;
112
+ private _states : CronState < T > ;
113
+ private fn ?: CronCallback < T > ;
113
114
constructor (
114
115
pattern : string | Date ,
115
- fnOrOptions1 ?: CronOptions | CronCallback ,
116
- fnOrOptions2 ?: CronOptions | CronCallback ,
116
+ fnOrOptions1 ?: CronOptions < T > | CronCallback < T > ,
117
+ fnOrOptions2 ?: CronOptions < T > | CronCallback < T > ,
117
118
) {
118
119
// Make options and func optional and interchangable
119
- let options , func ;
120
+ let options : CronOptions < T > | undefined ;
121
+ let func : CronCallback < T > | undefined ;
120
122
121
123
if ( isFunction ( fnOrOptions1 ) ) {
122
- func = fnOrOptions1 ;
124
+ func = fnOrOptions1 as CronCallback < T > ;
123
125
} else if ( typeof fnOrOptions1 === "object" ) {
124
- options = fnOrOptions1 ;
126
+ options = fnOrOptions1 as CronOptions < T > ;
125
127
} else if ( fnOrOptions1 !== void 0 ) {
126
128
throw new Error (
127
129
"Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options)." ,
128
130
) ;
129
131
}
130
132
131
133
if ( isFunction ( fnOrOptions2 ) ) {
132
- func = fnOrOptions2 ;
134
+ func = fnOrOptions2 as CronCallback < T > ;
133
135
} else if ( typeof fnOrOptions2 === "object" ) {
134
- options = fnOrOptions2 ;
136
+ options = fnOrOptions2 as CronOptions < T > ;
135
137
} else if ( fnOrOptions2 !== void 0 ) {
136
138
throw new Error (
137
139
"Cron: Invalid argument passed for funcIn. Should be one of function, or object (options)." ,
138
140
) ;
139
141
}
140
142
141
143
this . name = options ?. name ;
142
- this . options = CronOptionsHandler ( options ) ;
144
+ this . options = CronOptionsHandler < T > ( options ) ;
143
145
144
146
this . _states = {
145
147
kill : false ,
@@ -159,7 +161,7 @@ class Cron {
159
161
pattern &&
160
162
( pattern instanceof Date || ( ( typeof pattern === "string" ) && pattern . indexOf ( ":" ) > 0 ) )
161
163
) {
162
- this . _states . once = new CronDate ( pattern , this . options . timezone || this . options . utcOffset ) ;
164
+ this . _states . once = new CronDate < T > ( pattern , this . options . timezone || this . options . utcOffset ) ;
163
165
} else {
164
166
this . _states . pattern = new CronPattern ( pattern as string , this . options . timezone ) ;
165
167
}
@@ -172,7 +174,7 @@ class Cron {
172
174
"Cron: Tried to initialize new named job '" + this . name + "', but name already taken." ,
173
175
) ;
174
176
} else {
175
- scheduledJobs . push ( this ) ;
177
+ scheduledJobs . push ( this as Cron < unknown > ) ;
176
178
}
177
179
}
178
180
@@ -191,7 +193,7 @@ class Cron {
191
193
* @param prev - Optional. Date to start from. Can be a CronDate, Date object, or a string representing a date.
192
194
* @returns The next run time as a Date object, or null if there is no next run.
193
195
*/
194
- public nextRun ( prev ?: CronDate | Date | string | null ) : Date | null {
196
+ public nextRun ( prev ?: CronDate < T > | Date | string | null ) : Date | null {
195
197
const next = this . _next ( prev ) ;
196
198
return next ? next . getDate ( false ) : null ;
197
199
}
@@ -208,7 +210,8 @@ class Cron {
208
210
n = this . _states . maxRuns ;
209
211
}
210
212
const enumeration : Date [ ] = [ ] ;
211
- let prev : CronDate | Date | string | undefined | null = previous || this . _states . currentRun ||
213
+ let prev : CronDate < T > | Date | string | undefined | null = previous ||
214
+ this . _states . currentRun ||
212
215
undefined ;
213
216
while ( n -- && ( prev = this . nextRun ( prev ) ) ) {
214
217
enumeration . push ( prev ) ;
@@ -283,15 +286,15 @@ class Cron {
283
286
*
284
287
* @param prev Starting date, defaults to now - minimum interval
285
288
*/
286
- public msToNext ( prev ?: CronDate | Date | string ) : number | null {
289
+ public msToNext ( prev ?: CronDate < T > | Date | string ) : number | null {
287
290
// Get next run time
288
291
const next = this . _next ( prev ) ;
289
292
290
293
if ( next ) {
291
294
if ( prev instanceof CronDate || prev instanceof Date ) {
292
295
return ( next . getTime ( ) - prev . getTime ( ) ) ;
293
296
} else {
294
- return ( next . getTime ( ) - new CronDate ( prev ) . getTime ( ) ) ;
297
+ return ( next . getTime ( ) - new CronDate < T > ( prev ) . getTime ( ) ) ;
295
298
}
296
299
} else {
297
300
return null ;
@@ -317,7 +320,7 @@ class Cron {
317
320
318
321
// Remove job from the scheduledJobs array to free up the name, and allow the job to be
319
322
// garbage collected
320
- const jobIndex = scheduledJobs . indexOf ( this ) ;
323
+ const jobIndex = scheduledJobs . indexOf ( this as Cron < unknown > ) ;
321
324
if ( jobIndex >= 0 ) {
322
325
scheduledJobs . splice ( jobIndex , 1 ) ;
323
326
}
@@ -350,7 +353,7 @@ class Cron {
350
353
*
351
354
* @param func - Function to be run each iteration of pattern
352
355
*/
353
- public schedule ( func ?: CronCallback ) : Cron {
356
+ public schedule ( func ?: CronCallback < T > ) : Cron < T > {
354
357
// If a function is already scheduled, bail out
355
358
if ( func && this . fn ) {
356
359
throw new Error (
@@ -395,15 +398,15 @@ class Cron {
395
398
private async _trigger ( initiationDate ?: Date ) {
396
399
this . _states . blocking = true ;
397
400
398
- this . _states . currentRun = new CronDate (
401
+ this . _states . currentRun = new CronDate < T > (
399
402
void 0 , // We should use initiationDate, but that does not play well with fake timers in third party tests. In real world there is not much difference though */
400
403
this . options . timezone || this . options . utcOffset ,
401
404
) ;
402
405
403
406
if ( this . options . catch ) {
404
407
try {
405
408
if ( this . fn !== undefined ) {
406
- await this . fn ( this , this . options . context ) ;
409
+ await this . fn ( this , this . options . context as T ) ;
407
410
}
408
411
} catch ( _e ) {
409
412
if ( isFunction ( this . options . catch ) ) {
@@ -413,11 +416,11 @@ class Cron {
413
416
} else {
414
417
// Trigger the function without catching
415
418
if ( this . fn !== undefined ) {
416
- await this . fn ( this , this . options . context ) ;
419
+ await this . fn ( this , this . options . context as T ) ;
417
420
}
418
421
}
419
422
420
- this . _states . previousRun = new CronDate (
423
+ this . _states . previousRun = new CronDate < T > (
421
424
initiationDate ,
422
425
this . options . timezone || this . options . utcOffset ,
423
426
) ;
@@ -470,7 +473,7 @@ class Cron {
470
473
/**
471
474
* Internal version of next. Cron needs millseconds internally, hence _next.
472
475
*/
473
- private _next ( previousRun ?: CronDate | Date | string | null ) {
476
+ private _next ( previousRun ?: CronDate < T > | Date | string | null ) {
474
477
let hasPreviousRun = ( previousRun || this . _states . currentRun ) ? true : false ;
475
478
476
479
// If no previous run, and startAt and interval is set, calculate when the last run should have been
@@ -481,19 +484,19 @@ class Cron {
481
484
}
482
485
483
486
// Ensure previous run is a CronDate
484
- previousRun = new CronDate ( previousRun , this . options . timezone || this . options . utcOffset ) ;
487
+ previousRun = new CronDate < T > ( previousRun , this . options . timezone || this . options . utcOffset ) ;
485
488
486
489
// Previous run should never be before startAt
487
490
if (
488
491
this . options . startAt && previousRun &&
489
- previousRun . getTime ( ) < ( this . options . startAt as CronDate ) . getTime ( )
492
+ previousRun . getTime ( ) < ( this . options . startAt as CronDate < T > ) . getTime ( )
490
493
) {
491
494
previousRun = this . options . startAt ;
492
495
}
493
496
494
497
// Calculate next run according to pattern or one-off timestamp, pass actual previous run to increment
495
- let nextRun : CronDate | null = this . _states . once ||
496
- new CronDate ( previousRun , this . options . timezone || this . options . utcOffset ) ;
498
+ let nextRun : CronDate < T > | null = this . _states . once ||
499
+ new CronDate < T > ( previousRun , this . options . timezone || this . options . utcOffset ) ;
497
500
498
501
// if the startAt is in the future and the interval is set, then the prev is already set to the startAt, so there is no need to increment it
499
502
if ( ! startAtInFutureWithInterval && nextRun !== this . _states . once ) {
@@ -504,13 +507,15 @@ class Cron {
504
507
) ;
505
508
}
506
509
507
- if ( this . _states . once && this . _states . once . getTime ( ) <= ( previousRun as CronDate ) . getTime ( ) ) {
510
+ if (
511
+ this . _states . once && this . _states . once . getTime ( ) <= ( previousRun as CronDate < T > ) . getTime ( )
512
+ ) {
508
513
return null ;
509
514
} else if (
510
515
( nextRun === null ) ||
511
516
( this . _states . maxRuns !== undefined && this . _states . maxRuns <= 0 ) ||
512
517
( this . _states . kill ) ||
513
- ( this . options . stopAt && nextRun . getTime ( ) >= ( this . options . stopAt as CronDate ) . getTime ( ) )
518
+ ( this . options . stopAt && nextRun . getTime ( ) >= ( this . options . stopAt as CronDate < T > ) . getTime ( ) )
514
519
) {
515
520
return null ;
516
521
} else {
@@ -524,21 +529,22 @@ class Cron {
524
529
* Should only be called from the _next function.
525
530
*/
526
531
private _calculatePreviousRun (
527
- prev : CronDate | Date | string | undefined | null ,
532
+ prev : CronDate < T > | Date | string | undefined | null ,
528
533
hasPreviousRun : boolean ,
529
- ) : [ CronDate | undefined , boolean ] {
530
- const now = new CronDate ( undefined , this . options . timezone || this . options . utcOffset ) ;
531
- let newPrev : CronDate | undefined | null = prev as CronDate ;
532
- if ( ( this . options . startAt as CronDate ) . getTime ( ) <= now . getTime ( ) ) {
533
- newPrev = this . options . startAt as CronDate ;
534
- let prevTimePlusInterval = ( newPrev as CronDate ) . getTime ( ) + this . options . interval ! * 1000 ;
534
+ ) : [ CronDate < T > | undefined , boolean ] {
535
+ const now = new CronDate < T > ( undefined , this . options . timezone || this . options . utcOffset ) ;
536
+ let newPrev : CronDate < T > | undefined | null = prev as CronDate < T > ;
537
+ if ( ( this . options . startAt as CronDate < T > ) . getTime ( ) <= now . getTime ( ) ) {
538
+ newPrev = this . options . startAt as CronDate < T > ;
539
+ let prevTimePlusInterval = ( newPrev as CronDate < T > ) . getTime ( ) + this . options . interval ! * 1000 ;
535
540
while ( prevTimePlusInterval <= now . getTime ( ) ) {
536
- newPrev = new CronDate ( newPrev , this . options . timezone || this . options . utcOffset ) . increment (
537
- this . _states . pattern ,
538
- this . options ,
539
- true ,
540
- ) ;
541
- prevTimePlusInterval = ( newPrev as CronDate ) . getTime ( ) + this . options . interval ! * 1000 ;
541
+ newPrev = new CronDate < T > ( newPrev , this . options . timezone || this . options . utcOffset )
542
+ . increment (
543
+ this . _states . pattern ,
544
+ this . options ,
545
+ true ,
546
+ ) ;
547
+ prevTimePlusInterval = ( newPrev as CronDate < T > ) . getTime ( ) + this . options . interval ! * 1000 ;
542
548
}
543
549
hasPreviousRun = true ;
544
550
}
0 commit comments