Skip to content

Commit

Permalink
Merge pull request #54 from Codex-/Typings
Browse files Browse the repository at this point in the history
New/rewritten typings
  • Loading branch information
jessetane committed Dec 11, 2018
2 parents c76211c + 440ab61 commit acc924d
Show file tree
Hide file tree
Showing 7 changed files with 5,037 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.idea
node_modules
coverage
.vscode
test/bundle.js
coverage
199 changes: 160 additions & 39 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,168 @@
// TypeScript definition for queue
// Provided by Hendrik 'Xendo' Meyer <https://github.com/butterkekstorte>
// Licensed the same as the library itself
/*
Import via
import {IQueue, IQueueWorker} from 'queue';
var queue: IQueue = require('queue');
*/

interface IQueue{
(opts?:IQueueOptions): IQueue
push(...worker:IQueueWorker[]):number
start(callback?:(error?:Error) => void):void
stop():void
end(error?:Error):void
unshift(...worker:IQueueWorker[]):number
splice(index:number, deleteHowMany:number, ...worker:IQueueWorker[]):IQueue
pop():IQueueWorker|void
shift():IQueueWorker|void
slice(begin:number, end?:number):IQueue
reverse():IQueue
indexOf(searchElement:IQueueWorker, fromIndex?:number):number
lastIndexOf(searchElement:IQueueWorker, fromIndex?:number):number
concurrency:number
timeout:number
length:number
on(event:string, callback:IQueueEventCallback):void
// Type definitions for Queue 4.5.1
// Project: https://github.com/jessetane/queue
// Definitions by: Alex Miller <https://github.com/codex->

import { EventEmitter } from "events";

export interface Options {
/**
* Max number of jobs the queue should process concurrently.
*
* @default Infinity
*/
concurrency?: number;

/**
* Milliseconds to wait for a job to execute its callback.
*
* @default 0
*/
timeout?: number;

/**
* Ensures the queue is always running if jobs are available. Useful in situations where you are using a queue only for concurrency control.
*
* @default false
*/
autostart?: boolean;

/**
* An array to set job callback arguments on.
*
* @default null
*/
results?: any[];
}

interface IQueueOptions {
concurrency?:number
timeout?:number
autostart?:boolean
results?:Array<any>
interface Queue extends EventEmitter {
/**
* Max number of jobs the queue should process concurrently.
*/
concurrency: number;

/**
* Milliseconds to wait for a job to execute its callback.
*/
timeout: number;

/**
* Ensures the queue is always running if jobs are available.
*/
autostart: boolean;

/**
* An array to set job callback arguments on.
*/
results: any[] | null;

/**
* Jobs pending + jobs to process.
*/
readonly length: number;

/**
* Adds one or more elements to the end of the Queue and returns the new length of the Queue.
*
* @param workers New workers of the Queue.
*/
push(...workers: QueueWorker[]): number;

/**
* Adds one or more elements to the front of the Queue and returns the new length of the Queue.
*
* @param workers Workers to insert at the start of the Queue.
*/
unshift(...workers: QueueWorker[]): number;

/**
* Adds and/or removes elements from the queue.
*
* @param start The zero-based location in the Queue from which to start removing elements.
* @param deleteCount The number of elements to remove.
*/
splice(start: number, deleteCount?: number): Queue;

/**
* Adds and/or removes elements from the queue.
*
* @param start The zero-based location in the Queue from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @param workers Workers to insert into the Queue in place of the deleted elements.
*/
splice(start: number, deleteCount: number, ...workers: QueueWorker[]): Queue;

/**
* Removes the last element from the Queue and returns that element.
*/
pop(): QueueWorker | undefined;

/**
* Removes the first element from the Queue and returns that element.
*/
shift(): QueueWorker | undefined;

/**
* Extracts a section of the Queue and returns Queue.
*
* @param start The beginning of the specified portion of the Queue.
* @param end The end of the specified portion of the Queue.
*/
slice(start?: number, end?: number): Queue;

/**
* Reverses the order of the elements of the Queue in place.
*/
reverse(): Queue;

/**
* Returns the first (least) index of an element within the Queue equal to the specified value, or -1 if none is found.
*
* @param searchElement The value to locate in the Queue.
* @param fromIndex The Queue index at which to begin the search. If omitted, the search starts at index 0.
*/
indexOf(searchElement: QueueWorker, fromIndex?: number): number;

/**
* Returns the last (greatest) index of an element within the Queue equal to the specified value, or -1 if none is found.
*
* @param searchElement The value to locate in the Queue.
* @param fromIndex The Queue index at which to begin the search. If omitted, the search starts at the last index in the Queue.
*/
lastIndexOf(searchElement: QueueWorker, fromIndex?: number): number;

/**
* Starts the queue.
*
* @param callback Callback to be called when the queue empties or when an error occurs.
*/
start(callback?: (error?: Error) => void): void;

/**
* Stops the queue.
*/
stop(): void;

/**
* Stop and empty the queue immediately.
*
* @param error error of why the stop has occurred, to be passed to start callback if supplied.
*/
end(error?: Error): void;
}

interface IQueueWorker {
(cb?:(err?:Error, data?:Object) => void):void
interface QueueConstructor {
(options?: Options): Queue;
new (options?: Options): Queue;
}

interface IQueueEventCallback {
(data?:Object|Error|IQueueWorker, job?:IQueueWorker):void
declare const Queue: QueueConstructor;

export default Queue;

export interface QueueWorker {
(callback?: QueueWorkerCallback): void;
}

export {IQueue, IQueueEventCallback, IQueueOptions, IQueueWorker};
export interface QueueWorkerCallback {
(error?: Error, data?: Object): void;
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter

module.exports = Queue
module.exports.default = Queue

function Queue (options) {
if (!(this instanceof Queue)) {
Expand Down
44 changes: 44 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expectType } from 'tsd-check';
import Queue, { Options, QueueWorker, QueueWorkerCallback } from '.'

expectType<Options>({});
expectType<Options>({ concurrency: 0 });
expectType<Options>({ timeout: 0 });
expectType<Options>({ autostart: true });
expectType<Options>({ results: [0, 'a', true, undefined, NaN] });

expectType<QueueWorker>(() => undefined);
expectType<QueueWorker>((callback: QueueWorkerCallback) => undefined);

expectType<QueueWorkerCallback>(() => undefined);
expectType<QueueWorkerCallback>((data: Error) => undefined);
expectType<QueueWorkerCallback>((error: Error) => undefined);
expectType<QueueWorkerCallback>((error: Error, data: Object) => undefined);

expectType<Queue>(Queue());
expectType<Queue>(Queue({}));
expectType<Queue>(Queue({ concurrency: 0, timeout: 0, autostart: true, results: [] }));
expectType<Queue>(new Queue());
expectType<Queue>(new Queue({}));
expectType<Queue>(new Queue({ concurrency: 0, timeout: 0, autostart: true, results: [] }));

const q: Queue = Queue();

expectType<Queue>(q);
expectType<number>(q.push(() => { }));
expectType<number>(q.unshift(() => { }));
expectType<Queue>(q.splice(0));
expectType<Queue>(q.splice(0, 0, () => undefined));
expectType<QueueWorker>(q.pop());
expectType<QueueWorker>(q.shift());
expectType<Queue>(q.slice());
expectType<Queue>(q.slice(0));
expectType<Queue>(q.slice(0, 0));
expectType<Queue>(q.reverse());
expectType<number>(q.indexOf(() => { }));
expectType<number>(q.indexOf(() => { }, 0));
expectType<number>(q.lastIndexOf(() => { }));
expectType<number>(q.lastIndexOf(() => { }, 0));
expectType<void>(q.start());
expectType<void>(q.stop());
expectType<void>(q.end());

0 comments on commit acc924d

Please sign in to comment.