From 7cc27e8a787a9c77fc242ff6131f8fd88eb0311c Mon Sep 17 00:00:00 2001 From: Matanel Sindilevich Date: Tue, 7 Nov 2017 22:20:40 +0200 Subject: [PATCH 1/2] Embrace the module-function nature of tress --- types/tress/index.d.ts | 290 +++++++++++++++++++------------------ types/tress/tress-tests.ts | 2 +- types/tress/tslint.json | 3 +- 3 files changed, 150 insertions(+), 145 deletions(-) diff --git a/types/tress/index.d.ts b/types/tress/index.d.ts index a7237fbb98..ab4783746d 100644 --- a/types/tress/index.d.ts +++ b/types/tress/index.d.ts @@ -4,155 +4,157 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 -export type TressJobCallback = (this: TressJobData, ...args: any[]) => void; -export type TressWorkerDoneCallback = (err: boolean | Error | null | undefined, ...args: any[]) => void; +declare namespace tress { + export type TressJobCallback = (this: TressJobData, ...args: any[]) => void; + export type TressWorkerDoneCallback = (err: boolean | Error | null | undefined, ...args: any[]) => void; -export interface TressJobData { [name: string]: {}; } + export interface TressJobData { [name: string]: {}; } -export interface TressJob { - data: TressJobData; - callback: TressJobCallback; -} + export interface TressJob { + data: TressJobData; + callback: TressJobCallback; + } -export interface TressJobQueues { - failed: TressJobData[]; - finished: TressJobData[]; - waiting: TressJobData[]; -} + export interface TressJobQueues { + failed: TressJobData[]; + finished: TressJobData[]; + waiting: TressJobData[]; + } -export interface TressStatic { - // Properties + export interface TressStatic { + // Properties - /** - * Array of jobs currently being processed (readonly) - */ - readonly active: TressJob[]; - /** - * A minimum threshold buffer in order to say that the queue is unsaturated - */ - buffer: number; - /** - * This property for alter the concurrency/delay on-the-fly - */ - concurrency: number; - /** - * Array of failed jobs - * (the done callback was called from worker with error in first argument) (readonly) - */ - readonly failed: TressJob[]; - /** - * Array of correctly finished jobs - * (the done callback was called from worker with null or undefined (or any other false equivalent) in first argument) (readonly) - */ - readonly finished: TressJob[]; - /** - * A boolean for determining whether the queue is in a paused state. - * (readonly - use pause() and resume() instead) - */ - readonly paused: boolean; - /** - * false untill any items have been pushed and processed by the queue. - * Then becomes true and never changes in queue lifecycle (readonly) - */ - readonly started: boolean; - /** - * Array of queued jobs (readonly) - */ - readonly waiting: TressJob[]; + /** + * Array of jobs currently being processed (readonly) + */ + readonly active: TressJob[]; + /** + * A minimum threshold buffer in order to say that the queue is unsaturated + */ + buffer: number; + /** + * This property for alter the concurrency/delay on-the-fly + */ + concurrency: number; + /** + * Array of failed jobs + * (the done callback was called from worker with error in first argument) (readonly) + */ + readonly failed: TressJob[]; + /** + * Array of correctly finished jobs + * (the done callback was called from worker with null or undefined (or any other false equivalent) in first argument) (readonly) + */ + readonly finished: TressJob[]; + /** + * A boolean for determining whether the queue is in a paused state. + * (readonly - use pause() and resume() instead) + */ + readonly paused: boolean; + /** + * false untill any items have been pushed and processed by the queue. + * Then becomes true and never changes in queue lifecycle (readonly) + */ + readonly started: boolean; + /** + * Array of queued jobs (readonly) + */ + readonly waiting: TressJob[]; - // Methods + // Methods - /** - * Returns false if there are items waiting or being processed, - * or true if not - */ - idle(): boolean; - /** - * Removes the drain callback and empties remaining jobs from the queue - * forcing it to go idle - */ - kill(): void; - /** - * Returns the number of items waiting to be processed - */ - length(): number; - /** - * Loads new arrays from data object to waiting, failed, and finished arrays and sets active to empty array. - * Rises an error if started is true - */ - load(data: TressJobQueues): void; - /** - * Pauses the processing of jobs until resume() is called - */ - pause(): void; - /** - * Adds a new job to the queue. - * Instead of a single job, a jobs array can be submitted. - * Note, that if you pass callback as second argument, - * tress calls this callback once the worker has finished processing the job - */ - push(job: TressJobData | TressJobData[], done?: TressJobCallback): void; - /** - * Resumes the processing of queued jobs when the queue is paused - */ - resume(): void; - /** - * Returns the number of items currently being processed - */ - running(): number; - /** - * Runs a callback with object, that contains arrays of waiting, failed, and finished jobs. - * If there are any active jobs at the moment, they will be concatenated to waiting array - */ - save(callback: (data: TressJobQueues) => void): void; - /** - * Returns the status of job ("waiting", "running", "finished", "pending" or "missing") - */ - status(job: TressJob): "active" | "failed" | "finished" | "missing" | "waiting"; - /** - * Adds a new job to the front of the queue. - * Instead of a single job, a jobs array can be submitted. - * Note, that if you pass callback as second argument, - * tress calls this callback once the worker has finished processing the job - */ - unshift(job: TressJobData | TressJobData[], done?: TressJobCallback): void; - /** - * Returns the array of items currently being processed - */ - workersList(): TressStatic["active"]; + /** + * Returns false if there are items waiting or being processed, + * or true if not + */ + idle(): boolean; + /** + * Removes the drain callback and empties remaining jobs from the queue + * forcing it to go idle + */ + kill(): void; + /** + * Returns the number of items waiting to be processed + */ + length(): number; + /** + * Loads new arrays from data object to waiting, failed, and finished arrays and sets active to empty array. + * Rises an error if started is true + */ + load(data: TressJobQueues): void; + /** + * Pauses the processing of jobs until resume() is called + */ + pause(): void; + /** + * Adds a new job to the queue. + * Instead of a single job, a jobs array can be submitted. + * Note, that if you pass callback as second argument, + * tress calls this callback once the worker has finished processing the job + */ + push(job: TressJobData | TressJobData[], done?: TressJobCallback): void; + /** + * Resumes the processing of queued jobs when the queue is paused + */ + resume(): void; + /** + * Returns the number of items currently being processed + */ + running(): number; + /** + * Runs a callback with object, that contains arrays of waiting, failed, and finished jobs. + * If there are any active jobs at the moment, they will be concatenated to waiting array + */ + save(callback: (data: TressJobQueues) => void): void; + /** + * Returns the status of job ("waiting", "running", "finished", "pending" or "missing") + */ + status(job: TressJob): "active" | "failed" | "finished" | "missing" | "waiting"; + /** + * Adds a new job to the front of the queue. + * Instead of a single job, a jobs array can be submitted. + * Note, that if you pass callback as second argument, + * tress calls this callback once the worker has finished processing the job + */ + unshift(job: TressJobData | TressJobData[], done?: TressJobCallback): void; + /** + * Returns the array of items currently being processed + */ + workersList(): TressStatic["active"]; - // Callbacks + // Callbacks - /** - * A callback that is called when the last item from the queue has returned from the worker - */ - drain(): void; - /** - * A callback that is called when the last item from the queue is given to a worker - */ - empty(): void; - /** - * A callback that is called when job failed (worker call done with error as first argument). - * Note, that this callback is called after job has been moved from active to failed/finished and after job callback (from push/unshift) was called - */ - error(this: TressJobData, err: Error, job: TressJobData, ...args: any[]): void; - /** - * A callback that is called when job returned to queue (worker call done with boolean as first argument) - */ - retry(this: TressJobData, ...args: any[]): void; - /** - * A callback that is called when the number of running workers hits the concurrency limit, and further jobs will be queued - */ - saturated(): void; - /** - * A callback that is called when job correctly finished (worker call done with null or undefined as first argument). - * Note, that this callback is called after job has been moved from active to failed/finished and after job callback (from push/unshift) was called - */ - success(this: TressJobData, ...args: any[]): void; - /** - * A callback that is called when the number of running workers is less than the concurrency & buffer limits, and further jobs will not be queued - */ - unsaturated(): void; + /** + * A callback that is called when the last item from the queue has returned from the worker + */ + drain(): void; + /** + * A callback that is called when the last item from the queue is given to a worker + */ + empty(): void; + /** + * A callback that is called when job failed (worker call done with error as first argument). + * Note, that this callback is called after job has been moved from active to failed/finished and after job callback (from push/unshift) was called + */ + error(this: TressJobData, err: Error, job: TressJobData, ...args: any[]): void; + /** + * A callback that is called when job returned to queue (worker call done with boolean as first argument) + */ + retry(this: TressJobData, ...args: any[]): void; + /** + * A callback that is called when the number of running workers hits the concurrency limit, and further jobs will be queued + */ + saturated(): void; + /** + * A callback that is called when job correctly finished (worker call done with null or undefined as first argument). + * Note, that this callback is called after job has been moved from active to failed/finished and after job callback (from push/unshift) was called + */ + success(this: TressJobData, ...args: any[]): void; + /** + * A callback that is called when the number of running workers is less than the concurrency & buffer limits, and further jobs will not be queued + */ + unsaturated(): void; + } } /** @@ -169,6 +171,8 @@ export interface TressStatic { * should be run in parallel. If omitted, the concurrency defaults to 1. * If negative - no parallel and delay between worker functions (concurrency -1,000 sets 1 second delay) */ -export function tress( - worker: (job: TressJobData, done: TressWorkerDoneCallback) => void, - concurrency?: number): TressStatic; +declare function tress( + worker: (job: tress.TressJobData, done: tress.TressWorkerDoneCallback) => void, + concurrency?: number): tress.TressStatic; + +export = tress; diff --git a/types/tress/tress-tests.ts b/types/tress/tress-tests.ts index 6d76d7e40d..0d4bf8e882 100644 --- a/types/tress/tress-tests.ts +++ b/types/tress/tress-tests.ts @@ -1,6 +1,6 @@ /// -import { tress } from "tress"; +import * as tress from "tress"; function someAsyncFunction(job: any, callback: (err: any, data?: any) => void): void { const p = Promise.resolve(job) diff --git a/types/tress/tslint.json b/types/tress/tslint.json index 49b250ccb9..a67cd2c5e9 100644 --- a/types/tress/tslint.json +++ b/types/tress/tslint.json @@ -1,6 +1,7 @@ { "extends": "dtslint/dt.json", "rules": { - "space-before-function-paren": false + "space-before-function-paren": false, + "strict-export-declare-modifiers": false } } \ No newline at end of file From 94513f60cd19ae97a25e39c783c835f3c9c90e6d Mon Sep 17 00:00:00 2001 From: Matanel Sindilevich Date: Wed, 8 Nov 2017 07:16:06 +0200 Subject: [PATCH 2/2] Importing using the CommonJS-style, as per https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-function-d-ts.html --- types/tress/tress-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/tress/tress-tests.ts b/types/tress/tress-tests.ts index 0d4bf8e882..bf2667ec85 100644 --- a/types/tress/tress-tests.ts +++ b/types/tress/tress-tests.ts @@ -1,6 +1,6 @@ /// -import * as tress from "tress"; +import tress = require("tress"); function someAsyncFunction(job: any, callback: (err: any, data?: any) => void): void { const p = Promise.resolve(job)