Skip to content

Commit 0c9fbb9

Browse files
authored
Merge 19aecb4 into master
2 parents a450e73 + 19aecb4 commit 0c9fbb9

File tree

10 files changed

+2194
-3475
lines changed

10 files changed

+2194
-3475
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"parser": "babel-eslint",
33

4-
"extends": ["standard", "prettier", "prettier/flowtype", "prettier/standard"],
4+
"extends": ["standard", "prettier"],
55
"plugins": ["flowtype", "standard", "prettier"],
66

77
"env": {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ Whether the queue is running.
174174

175175
Whether the queue has been forced to stop by calling `Queue.stop`.
176176

177+
#### **public** `.size`
178+
179+
Size of the queue.
180+
177181
#### **public** `.isEmpty`
178182

179183
Whether the queue is empty, i.e. there's no tasks.

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ declare module "queue-promise" {
1919
class Queue extends EventEmitter {
2020
readonly options: QueueOptions;
2121
readonly state: State;
22+
readonly size: number;
2223
readonly isEmpty: boolean;
2324
readonly shouldRun: boolean;
2425

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.flow

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const State = {
77
STOPPED: 2,
88
};
99

10+
type Options = {
11+
concurrent: number,
12+
interval: number,
13+
start: boolean,
14+
};
15+
1016
/**
1117
* A small and simple library for promise-based queues. It will execute enqueued
1218
* functions concurrently at a specified speed. When a task is being resolved or
@@ -43,7 +49,7 @@ export default class Queue extends EventEmitter {
4349
* @type {number} Used to generate a unique id for each task
4450
* @access private
4551
*/
46-
uniqueId = 0;
52+
uniqueId: number = 0;
4753

4854
/**
4955
* @type {number}
@@ -61,7 +67,7 @@ export default class Queue extends EventEmitter {
6167
* @type {number} Amount of tasks currently handled by the queue
6268
* @access private
6369
*/
64-
currentlyHandled = 0;
70+
currentlyHandled: number = 0;
6571

6672
/**
6773
* @type {State}
@@ -76,7 +82,7 @@ export default class Queue extends EventEmitter {
7682
* @type {boolean} options.start Whether it should automatically execute new tasks as soon as they are added
7783
* @access public
7884
*/
79-
options = {
85+
options: Options = {
8086
concurrent: 5,
8187
interval: 500,
8288
start: true,
@@ -91,17 +97,12 @@ export default class Queue extends EventEmitter {
9197
* @param {boolean} options.start Whether it should automatically execute new tasks as soon as they are added
9298
* @return {Queue}
9399
*/
94-
constructor(options: Object = {}) {
100+
constructor(options: Options = {}) {
95101
super();
96102

97103
this.options = { ...this.options, ...options };
98104
this.options.interval = parseInt(this.options.interval, 10);
99105
this.options.concurrent = parseInt(this.options.concurrent, 10);
100-
101-
// Backward compatibility:
102-
if (options.concurrency) {
103-
this.options.concurrent = parseInt(options.concurrency, 10);
104-
}
105106
}
106107

107108
/**
@@ -111,7 +112,7 @@ export default class Queue extends EventEmitter {
111112
* @return {void}
112113
* @access public
113114
*/
114-
start() {
115+
start(): void {
115116
if (this.state !== State.RUNNING && !this.isEmpty) {
116117
this.state = State.RUNNING;
117118
this.emit("start");
@@ -132,7 +133,7 @@ export default class Queue extends EventEmitter {
132133
* @return {void}
133134
* @access public
134135
*/
135-
stop() {
136+
stop(): void {
136137
clearTimeout(this.timeoutId);
137138

138139
this.state = State.STOPPED;
@@ -146,7 +147,7 @@ export default class Queue extends EventEmitter {
146147
* @return {void}
147148
* @access private
148149
*/
149-
finalize() {
150+
finalize(): void {
150151
this.currentlyHandled -= 1;
151152

152153
if (this.currentlyHandled === 0 && this.isEmpty) {
@@ -171,7 +172,7 @@ export default class Queue extends EventEmitter {
171172
* @emits dequeue
172173
* @access private
173174
*/
174-
async execute() {
175+
async execute(): Promise<*> {
175176
const promises = [];
176177

177178
this.tasks.forEach((promise, id) => {
@@ -217,7 +218,7 @@ export default class Queue extends EventEmitter {
217218
* @emits dequeue
218219
* @access public
219220
*/
220-
dequeue() {
221+
dequeue(): Promise<*> {
221222
const { interval } = this.options;
222223

223224
return new Promise<*>((resolve, reject) => {
@@ -241,7 +242,7 @@ export default class Queue extends EventEmitter {
241242
* @return {void}
242243
* @access public
243244
*/
244-
enqueue(tasks: Function | Array<Function>) {
245+
enqueue(tasks: Function | Array<Function>): void {
245246
if (Array.isArray(tasks)) {
246247
tasks.map((task) => this.enqueue(task));
247248
return;
@@ -265,7 +266,7 @@ export default class Queue extends EventEmitter {
265266
* @see enqueue
266267
* @access public
267268
*/
268-
add(tasks: Function | Array<Function>) {
269+
add(tasks: Function | Array<Function>): void {
269270
this.enqueue(tasks);
270271
}
271272

@@ -275,18 +276,28 @@ export default class Queue extends EventEmitter {
275276
* @return {void}
276277
* @access public
277278
*/
278-
clear() {
279+
clear(): void {
279280
this.tasks.clear();
280281
}
281282

283+
/**
284+
* Size of the queue.
285+
*
286+
* @type {number}
287+
* @access public
288+
*/
289+
get size(): number {
290+
return this.tasks.size;
291+
}
292+
282293
/**
283294
* Checks whether the queue is empty, i.e. there's no tasks.
284295
*
285296
* @type {boolean}
286297
* @access public
287298
*/
288-
get isEmpty() {
289-
return this.tasks.size === 0;
299+
get isEmpty(): boolean {
300+
return this.size === 0;
290301
}
291302

292303
/**
@@ -295,7 +306,7 @@ export default class Queue extends EventEmitter {
295306
* @type {boolean}
296307
* @access public
297308
*/
298-
get shouldRun() {
309+
get shouldRun(): boolean {
299310
return !this.isEmpty && this.state !== State.STOPPED;
300311
}
301312
}

0 commit comments

Comments
 (0)