From 6819a981ddf548ea84b7dcd5078a4ebad8188a9a Mon Sep 17 00:00:00 2001 From: code-xhyun Date: Thu, 2 May 2024 11:24:57 +0900 Subject: [PATCH 1/2] fix: jobAttributes interface to use generic type T in data property --- src/job/index.ts | 2 +- src/pulse/create.ts | 4 ++-- src/pulse/define.ts | 4 ++-- src/pulse/every.ts | 8 ++++---- src/pulse/now.ts | 6 +++--- src/pulse/schedule.ts | 16 ++++++++++------ 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/job/index.ts b/src/job/index.ts index 5705417..81f4689 100644 --- a/src/job/index.ts +++ b/src/job/index.ts @@ -65,7 +65,7 @@ export interface JobAttributes /** * The job details. */ - data: any; + data: T; uniqueQuery?: any; uniqueOpts?: { diff --git a/src/pulse/create.ts b/src/pulse/create.ts index 0ed3d0e..b089e9f 100644 --- a/src/pulse/create.ts +++ b/src/pulse/create.ts @@ -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 = (name: string, data: T) => Job; +export type CreateMethod = (name: string, data: T) => Job; /** * Given a name and some data, create a new job * @name Pulse#create diff --git a/src/pulse/define.ts b/src/pulse/define.ts index b007f48..da5847b 100644 --- a/src/pulse/define.ts +++ b/src/pulse/define.ts @@ -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'); @@ -43,7 +43,7 @@ export interface DefineOptions { export type Processor = (job: Job, done?: () => void) => void; -export type DefineMethod = ( +export type DefineMethod = ( name: string, processor: Processor, options?: DefineOptions diff --git a/src/pulse/every.ts b/src/pulse/every.ts index 7f90efd..9b44e0f 100644 --- a/src/pulse/every.ts +++ b/src/pulse/every.ts @@ -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 = ( +export type EveryMethod = ( interval: string, names: string | string[], data?: T, @@ -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 ( + const createJob = async ( interval: string, name: string, data?: T, @@ -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 ( + const createJobs = async ( interval: string, names: string[], data?: T, diff --git a/src/pulse/now.ts b/src/pulse/now.ts index 9540947..7555352 100644 --- a/src/pulse/now.ts +++ b/src/pulse/now.ts @@ -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 = (name: string, data?: T) => Promise; +export type NowMethod = (name: string, data?: T) => Promise; /** * Create a job for this exact moment * @name Pulse#now @@ -15,7 +15,7 @@ export type NowMethod = (name: string, data?: T) => Promise; 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(); diff --git a/src/pulse/schedule.ts b/src/pulse/schedule.ts index 9dd3f6d..a999d75 100644 --- a/src/pulse/schedule.ts +++ b/src/pulse/schedule.ts @@ -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 = ( +export type ScheduleMethod = ( when: string | Date, names: string | string[], data?: T @@ -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 => { + const createJob = async (when: string | Date, name: string, data: T): Promise => { const job = this.create(name, data); await job.schedule(when).save(); @@ -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 => { + const createJobs = async ( + when: string | Date, + names: string[], + data: T + ): Promise => { try { const createJobList: Array> = []; names.map((name) => createJobList.push(createJob(when, name, data))); @@ -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'); From 324401a94b667d0b71df30a3ca9b3fb0f15afcad Mon Sep 17 00:00:00 2001 From: code-xhyun Date: Thu, 2 May 2024 11:25:46 +0900 Subject: [PATCH 2/2] fix: update type definition for Processor in define.ts --- src/pulse/define.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pulse/define.ts b/src/pulse/define.ts index da5847b..bc12121 100644 --- a/src/pulse/define.ts +++ b/src/pulse/define.ts @@ -41,7 +41,7 @@ export interface DefineOptions { shouldSaveResult?: boolean; } -export type Processor = (job: Job, done?: () => void) => void; +export type Processor = (job: Job, done: () => void) => void | Promise; export type DefineMethod = ( name: string,