From 4bcbb7024088a52cfab69e507d44bb604b35424c Mon Sep 17 00:00:00 2001 From: David Koblas Date: Tue, 5 Jun 2018 16:59:15 -0400 Subject: [PATCH 1/2] Add generic types to Queue and Job --- types/bull/bull-tests.tsx | 2 +- types/bull/index.d.ts | 43 ++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/types/bull/bull-tests.tsx b/types/bull/bull-tests.tsx index 765be54733..c2e0d3161f 100644 --- a/types/bull/bull-tests.tsx +++ b/types/bull/bull-tests.tsx @@ -7,7 +7,7 @@ import Queue = require("bull"); const videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379'); const audioQueue = new Queue('audio transcoding', {redis: {port: 6379, host: '127.0.0.1'}}); // Specify Redis connection using object -const imageQueue = new Queue('image transcoding'); +const imageQueue: Queue.Queue<{ image: string }> = new Queue('image transcoding'); videoQueue.process((job, done) => { // job.data contains the custom data passed when the job was created diff --git a/types/bull/index.d.ts b/types/bull/index.d.ts index dd97de15ac..83cc4a69bd 100644 --- a/types/bull/index.d.ts +++ b/types/bull/index.d.ts @@ -6,6 +6,7 @@ // Weeco // Gabriel Terwesten // Oleg Repin +// David Koblas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -87,13 +88,13 @@ declare namespace Bull { type JobId = number | string; - interface Job { + interface Job { id: JobId; /** * The custom data passed when the job was created */ - data: any; + data: T; /** * How many attempts where made to run this job @@ -258,7 +259,7 @@ declare namespace Bull { next: number; } - interface Queue { + interface Queue { /** * Returns a promise that resolves when Redis is connected and the queue is ready to accept jobs. * This replaces the `ready` event emitted on Queue in previous verisons. @@ -276,7 +277,7 @@ declare namespace Bull { * Errors will be passed as a second argument to the "failed" event; * results, as a second argument to the "completed" event. */ - process(callback: (job: Job, done: DoneCallback) => void): void; + process(callback: (job: Job, done: DoneCallback) => void): void; /** * Defines a processing function for the jobs placed into a given Queue. @@ -294,7 +295,7 @@ declare namespace Bull { * If the promise is rejected, the error will be passed as a second argument to the "failed" event. * If it is resolved, its value will be the "completed" event's second argument. */ - process(callback: ((job: Job) => void) | string): Promise; + process(callback: ((job: Job) => void) | string): Promise; /** * Defines a processing function for the jobs placed into a given Queue. @@ -314,7 +315,7 @@ declare namespace Bull { * * @param concurrency Bull will then call you handler in parallel respecting this max number. */ - process(concurrency: number, callback: ((job: Job) => void) | string): Promise; + process(concurrency: number, callback: ((job: Job) => void) | string): Promise; /** * Defines a processing function for the jobs placed into a given Queue. @@ -329,7 +330,7 @@ declare namespace Bull { * * @param concurrency Bull will then call you handler in parallel respecting this max number. */ - process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void; + process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void; /** * Defines a named processing function for the jobs placed into a given Queue. @@ -350,7 +351,7 @@ declare namespace Bull { * @param name Bull will only call the handler if the job name matches */ // tslint:disable-next-line:unified-signatures - process(name: string, callback: ((job: Job) => void) | string): Promise; + process(name: string, callback: ((job: Job) => void) | string): Promise; /** * Defines a processing function for the jobs placed into a given Queue. @@ -366,7 +367,7 @@ declare namespace Bull { * @param name Bull will only call the handler if the job name matches */ // tslint:disable-next-line:unified-signatures - process(name: string, callback: (job: Job, done: DoneCallback) => void): void; + process(name: string, callback: (job: Job, done: DoneCallback) => void): void; /** * Defines a named processing function for the jobs placed into a given Queue. @@ -387,7 +388,7 @@ declare namespace Bull { * @param name Bull will only call the handler if the job name matches * @param concurrency Bull will then call you handler in parallel respecting this max number. */ - process(name: string, concurrency: number, callback: ((job: Job) => void) | string): Promise; + process(name: string, concurrency: number, callback: ((job: Job) => void) | string): Promise; /** * Defines a processing function for the jobs placed into a given Queue. @@ -403,21 +404,21 @@ declare namespace Bull { * @param name Bull will only call the handler if the job name matches * @param concurrency Bull will then call you handler in parallel respecting this max number. */ - process(name: string, concurrency: number, callback: (job: Job, done: DoneCallback) => void): void; + process(name: string, concurrency: number, callback: (job: Job, done: DoneCallback) => void): void; /** * Creates a new job and adds it to the queue. * If the queue is empty the job will be executed directly, * otherwise it will be placed in the queue and executed as soon as possible. */ - add(data: any, opts?: JobOptions): Promise; + add(data: T, opts?: JobOptions): Promise>; /** * Creates a new named job and adds it to the queue. * If the queue is empty the job will be executed directly, * otherwise it will be placed in the queue and executed as soon as possible. */ - add(name: string, data: any, opts?: JobOptions): Promise; + add(name: string, data: T, opts?: JobOptions): Promise>; /** * Returns a promise that resolves when the queue is paused. @@ -466,32 +467,32 @@ declare namespace Bull { * Returns a promise that will return the job instance associated with the jobId parameter. * If the specified job cannot be located, the promise callback parameter will be set to null. */ - getJob(jobId: JobId): Promise; + getJob(jobId: JobId): Promise>; /** * Returns a promise that will return an array with the waiting jobs between start and end. */ - getWaiting(start?: number, end?: number): Promise; + getWaiting(start?: number, end?: number): Promise>>; /** * Returns a promise that will return an array with the active jobs between start and end. */ - getActive(start?: number, end?: number): Promise; + getActive(start?: number, end?: number): Promise>>; /** * Returns a promise that will return an array with the delayed jobs between start and end. */ - getDelayed(start?: number, end?: number): Promise; + getDelayed(start?: number, end?: number): Promise>>; /** * Returns a promise that will return an array with the completed jobs between start and end. */ - getCompleted(start?: number, end?: number): Promise; + getCompleted(start?: number, end?: number): Promise>>; /** * Returns a promise that will return an array with the failed jobs between start and end. */ - getFailed(start?: number, end?: number): Promise; + getFailed(start?: number, end?: number): Promise>>; /** * Returns JobInformation of repeatable jobs (ordered descending). Provide a start and/or an end @@ -502,7 +503,7 @@ declare namespace Bull { /** * ??? */ - nextRepeatableJob(name: string, data: any, opts: JobOptions): Promise; + nextRepeatableJob(name: string, data: any, opts: JobOptions): Promise>; /** * Removes a given repeatable job. The RepeatOptions and JobId needs to be the same as the ones @@ -565,7 +566,7 @@ declare namespace Bull { * @param status Status of the job to clean. Values are completed, wait, active, delayed, and failed. Defaults to completed. * @param limit Maximum amount of jobs to clean per call. If not provided will clean all matching jobs. */ - clean(grace: number, status?: JobStatus, limit?: number): Promise; + clean(grace: number, status?: JobStatus, limit?: number): Promise>>; /** * Listens to queue events From 60009ab2a6a13daae68e6e6e7b1b3a1c709f30af Mon Sep 17 00:00:00 2001 From: David Koblas Date: Tue, 5 Jun 2018 17:44:52 -0400 Subject: [PATCH 2/2] Updated the the event handlers with generic T --- types/bull/index.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/types/bull/index.d.ts b/types/bull/index.d.ts index 83cc4a69bd..cdef279acb 100644 --- a/types/bull/index.d.ts +++ b/types/bull/index.d.ts @@ -581,28 +581,28 @@ declare namespace Bull { /** * A job has started. You can use `jobPromise.cancel()` to abort it */ - on(event: 'active', callback: ActiveEventCallback): this; + on(event: 'active', callback: ActiveEventCallback): this; /** * A job has been marked as stalled. * This is useful for debugging job workers that crash or pause the event loop. */ - on(event: 'stalled', callback: StalledEventCallback): this; + on(event: 'stalled', callback: StalledEventCallback): this; /** * A job's progress was updated */ - on(event: 'progress', callback: ProgressEventCallback): this; + on(event: 'progress', callback: ProgressEventCallback): this; /** * A job successfully completed with a `result` */ - on(event: 'completed', callback: CompletedEventCallback): this; + on(event: 'completed', callback: CompletedEventCallback): this; /** * A job failed with `err` as the reason */ - on(event: 'failed', callback: FailedEventCallback): this; + on(event: 'failed', callback: FailedEventCallback): this; /** * The queue has been paused @@ -620,7 +620,7 @@ declare namespace Bull { * * @see Queue#clean() for details */ - on(event: 'cleaned', callback: CleanedEventCallback): this; + on(event: 'cleaned', callback: CleanedEventCallback): this; } type EventCallback = () => void; @@ -634,17 +634,17 @@ declare namespace Bull { cancel(): void; } - type ActiveEventCallback = (job: Job, jobPromise?: JobPromise) => void; + type ActiveEventCallback = (job: Job, jobPromise?: JobPromise) => void; - type StalledEventCallback = (job: Job) => void; + type StalledEventCallback = (job: Job) => void; - type ProgressEventCallback = (job: Job, progress: any) => void; + type ProgressEventCallback = (job: Job, progress: any) => void; - type CompletedEventCallback = (job: Job, result: any) => void; + type CompletedEventCallback = (job: Job, result: any) => void; - type FailedEventCallback = (job: Job, error: Error) => void; + type FailedEventCallback = (job: Job, error: Error) => void; - type CleanedEventCallback = (jobs: Job[], status: JobStatus) => void; + type CleanedEventCallback = (jobs: Array>, status: JobStatus) => void; } export = Bull;