Skip to content

Commit

Permalink
fix: Added missing template types for returned s in the method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kostysh committed May 31, 2024
1 parent d8b4c04 commit 59dcee6
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/pulse/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { Job, JobAttributesData } from '../job';

const debug = createDebugger('pulse:schedule');

export type ScheduleMethod = <T extends JobAttributesData>(
when: string | Date,
names: string | string[],
data?: T
) => Promise<Job | Job[]>;
/**
* Schedule a job or jobs at a specific time
* @name Pulse#schedule
Expand All @@ -18,15 +13,20 @@ export type ScheduleMethod = <T extends JobAttributesData>(
* @param data data to send to job
* @returns job or jobs created
*/
export const schedule: ScheduleMethod = function schedule(this: Pulse, when, names, data) {
export const schedule = function schedule<T extends JobAttributesData>(
this: Pulse,
when: string | Date,
names: string | string[],
data?: T,
) {
/**
* Internal method that creates a job with given date
* @param when when the job gets run
* @param name of job to run
* @param data data to send to job
* @returns instance of new job
*/
const createJob = async <T extends JobAttributesData>(when: string | Date, name: string, data: T): Promise<Job> => {
const createJob = async <T extends JobAttributesData>(when: string | Date, name: string, data: T): Promise<Job<T>> => {
const job = this.create(name, data);

await job.schedule(when).save();
Expand All @@ -45,9 +45,9 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
when: string | Date,
names: string[],
data: T
): Promise<Job[]> => {
): Promise<Job<T>[]> => {
try {
const createJobList: Array<Promise<Job>> = [];
const createJobList: Array<Promise<Job<T>>> = [];
names.map((name) => createJobList.push(createJob(when, name, data)));
debug('Pulse.schedule()::createJobs() -> all jobs created successfully');
return Promise.all(createJobList);
Expand All @@ -59,7 +59,7 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam

if (typeof names === 'string') {
debug('Pulse.schedule(%s, %O, [%O], cb)', when, names);
return createJob(when, names, data || {});
return createJob(when, names, data || {} as T);
}

if (Array.isArray(names)) {
Expand All @@ -69,3 +69,5 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam

throw new TypeError('Name must be string or array of strings');
};

export type ScheduleMethod = typeof schedule;

0 comments on commit 59dcee6

Please sign in to comment.