@@ -4,11 +4,6 @@ import { Job, JobAttributesData } from '../job';
4
4
5
5
const debug = createDebugger ( 'pulse:schedule' ) ;
6
6
7
- export type ScheduleMethod = < T extends JobAttributesData > (
8
- when : string | Date ,
9
- names : string | string [ ] ,
10
- data ?: T
11
- ) => Promise < Job | Job [ ] > ;
12
7
/**
13
8
* Schedule a job or jobs at a specific time
14
9
* @name Pulse#schedule
@@ -18,15 +13,20 @@ export type ScheduleMethod = <T extends JobAttributesData>(
18
13
* @param data data to send to job
19
14
* @returns job or jobs created
20
15
*/
21
- export const schedule : ScheduleMethod = function schedule ( this : Pulse , when , names , data ) {
16
+ export const schedule = function schedule < T extends JobAttributesData > (
17
+ this : Pulse ,
18
+ when : string | Date ,
19
+ names : string | string [ ] ,
20
+ data ?: T ,
21
+ ) {
22
22
/**
23
23
* Internal method that creates a job with given date
24
24
* @param when when the job gets run
25
25
* @param name of job to run
26
26
* @param data data to send to job
27
27
* @returns instance of new job
28
28
*/
29
- const createJob = async < T extends JobAttributesData > ( when : string | Date , name : string , data : T ) : Promise < Job > => {
29
+ const createJob = async < T extends JobAttributesData > ( when : string | Date , name : string , data : T ) : Promise < Job < T > > => {
30
30
const job = this . create ( name , data ) ;
31
31
32
32
await job . schedule ( when ) . save ( ) ;
@@ -45,9 +45,9 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
45
45
when : string | Date ,
46
46
names : string [ ] ,
47
47
data : T
48
- ) : Promise < Job [ ] > => {
48
+ ) : Promise < Job < T > [ ] > => {
49
49
try {
50
- const createJobList : Array < Promise < Job > > = [ ] ;
50
+ const createJobList : Array < Promise < Job < T > > > = [ ] ;
51
51
names . map ( ( name ) => createJobList . push ( createJob ( when , name , data ) ) ) ;
52
52
debug ( 'Pulse.schedule()::createJobs() -> all jobs created successfully' ) ;
53
53
return Promise . all ( createJobList ) ;
@@ -59,7 +59,7 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
59
59
60
60
if ( typeof names === 'string' ) {
61
61
debug ( 'Pulse.schedule(%s, %O, [%O], cb)' , when , names ) ;
62
- return createJob ( when , names , data || { } ) ;
62
+ return createJob ( when , names , data || { } as T ) ;
63
63
}
64
64
65
65
if ( Array . isArray ( names ) ) {
@@ -69,3 +69,5 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
69
69
70
70
throw new TypeError ( 'Name must be string or array of strings' ) ;
71
71
} ;
72
+
73
+ export type ScheduleMethod = typeof schedule ;
0 commit comments