Skip to content

Commit

Permalink
fix: jobAttributes interface to use generic type T in data property (#15
Browse files Browse the repository at this point in the history
)

* fix: jobAttributes interface to use generic type T in data property

* fix: update type definition for Processor in define.ts
  • Loading branch information
code-xhyun authored May 2, 2024
1 parent 52bd63f commit daee70a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/job/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface JobAttributes<T extends JobAttributesData = JobAttributesData>
/**
* The job details.
*/
data: any;
data: T;

uniqueQuery?: any;
uniqueOpts?: {
Expand Down
4 changes: 2 additions & 2 deletions src/pulse/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job } from '../job';
import { Job, JobAttributesData } from '../job';

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

export type CreateMethod = <T extends any>(name: string, data: T) => Job;
export type CreateMethod = <T extends JobAttributesData>(name: string, data: T) => Job;
/**
* Given a name and some data, create a new job
* @name Pulse#create
Expand Down
6 changes: 3 additions & 3 deletions src/pulse/define.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job, JobAttributes } from '../job';
import { Job, JobAttributesData } from '../job';

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

Expand Down Expand Up @@ -41,9 +41,9 @@ export interface DefineOptions {
shouldSaveResult?: boolean;
}

export type Processor<T extends JobAttributes> = (job: Job<T>, done?: () => void) => void;
export type Processor<T extends JobAttributesData> = (job: Job<T>, done: () => void) => void | Promise<void>;

export type DefineMethod = <T extends JobAttributes>(
export type DefineMethod = <T extends JobAttributesData>(
name: string,
processor: Processor<T>,
options?: DefineOptions
Expand Down
8 changes: 4 additions & 4 deletions src/pulse/every.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job } from '../job';
import { Job, JobAttributesData } from '../job';
import { JobOptions } from '../job/repeat-every';

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

export type EveryMethod = <T extends any>(
export type EveryMethod = <T extends JobAttributesData>(
interval: string,
names: string | string[],
data?: T,
Expand All @@ -31,7 +31,7 @@ export const every: EveryMethod = async function (this: Pulse, interval, names,
* @param [options] options to run job for
* @returns instance of job
*/
const createJob = async <T extends any>(
const createJob = async <T extends JobAttributesData>(
interval: string,
name: string,
data?: T,
Expand All @@ -52,7 +52,7 @@ export const every: EveryMethod = async function (this: Pulse, interval, names,
* @param [options] options to run job for
* @return array of jobs created
*/
const createJobs = async <T extends any>(
const createJobs = async <T extends JobAttributesData>(
interval: string,
names: string[],
data?: T,
Expand Down
6 changes: 3 additions & 3 deletions src/pulse/now.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job } from '../job';
import { Job, JobAttributesData } from '../job';

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

export type NowMethod = <T extends any>(name: string, data?: T) => Promise<Job>;
export type NowMethod = <T extends JobAttributesData>(name: string, data?: T) => Promise<Job>;
/**
* Create a job for this exact moment
* @name Pulse#now
Expand All @@ -15,7 +15,7 @@ export type NowMethod = <T extends any>(name: string, data?: T) => Promise<Job>;
export const now: NowMethod = async function (this: Pulse, name, data) {
debug('Pulse.now(%s, [Object])', name);
try {
const job = this.create(name, data);
const job = this.create(name, data || {});

job.schedule(new Date());
await job.save();
Expand Down
16 changes: 10 additions & 6 deletions src/pulse/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job } from '../job';
import { Job, JobAttributesData } from '../job';

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

export type ScheduleMethod = <T extends any>(
export type ScheduleMethod = <T extends JobAttributesData>(
when: string | Date,
names: string | string[],
data?: T
Expand All @@ -26,7 +26,7 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
* @param data data to send to job
* @returns instance of new job
*/
const createJob = async (when: string | Date, name: string, data: any): Promise<Job> => {
const createJob = async <T extends JobAttributesData>(when: string | Date, name: string, data: T): Promise<Job> => {
const job = this.create(name, data);

await job.schedule(when).save();
Expand All @@ -41,7 +41,11 @@ export const schedule: ScheduleMethod = function schedule(this: Pulse, when, nam
* @param data data to send to job
* @returns jobs that were created
*/
const createJobs = async (when: string | Date, names: string[], data: any): Promise<Job[]> => {
const createJobs = async <T extends JobAttributesData>(
when: string | Date,
names: string[],
data: T
): Promise<Job[]> => {
try {
const createJobList: Array<Promise<Job>> = [];
names.map((name) => createJobList.push(createJob(when, name, data)));
Expand All @@ -55,12 +59,12 @@ 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 || {});
}

if (Array.isArray(names)) {
debug('Pulse.schedule(%s, %O, [%O])', when, names);
return createJobs(when, names, data);
return createJobs(when, names, data || {});
}

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

0 comments on commit daee70a

Please sign in to comment.