-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.d.ts
201 lines (163 loc) · 5.95 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Type definitions for Queue
// Project: https://github.com/jessetane/queue
// Definitions by: Alex Miller <https://github.com/codex->
// Additions by Maksim Lavrenyuk <https://github.com/MaksimLavrenyuk>
type EventsMap = {
end: { error?: Error }
error: { error: Error, job: QueueWorker }
timeout: { next: (err?: Error, ...result: any[]) => void, job: QueueWorker }
success: { result: any[] }
start: { job?: QueueWorker }
}
export class QueueEvent<Name extends keyof EventsMap, Detail extends EventsMap[Name]> extends Event {
readonly detail: Detail
constructor(name: Name, detail: Detail)
}
type EventListenerOrEventListenerObject<Event extends QueueEvent<keyof EventsMap, EventsMap[keyof EventsMap]>> = (event: Event) => void | {
handleEvent(Event: Event): void;
};
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[];
}
export default class Queue extends EventTarget {
constructor(options?: Options)
/**
* 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, results?: any[] | null) => void): void;
start(): Promise<{ error?: Error, results?: any[] | null }>;
start(): 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;
addEventListener<Event extends keyof EventsMap>(name: Event, callback: EventListenerOrEventListenerObject<QueueEvent<Event, EventsMap[Event]>>, options?: AddEventListenerOptions | boolean): void;
dispatchEvent<Event extends keyof EventsMap>(event: QueueEvent<Event, EventsMap[Event]>): boolean;
removeEventListener<Event extends keyof EventsMap>(name: Event, callback: EventListenerOrEventListenerObject<QueueEvent<Event, EventsMap[Event]>>, options?: EventListenerOptions | boolean): void;
}
export interface QueueWorker {
(callback?: QueueWorkerCallback): void | Promise<any>;
/**
* Override queue timeout.
*/
timeout?: number;
/**
* If the QueueWorker returns a promise, it will be moved to this field.
* This can be useful when tracking timeout events
*/
promise?: Promise<any>
}
export interface QueueWorkerCallback {
(error?: Error, data?: Object): void;
}