Skip to content

Commit 3b5970c

Browse files
committed
add indication if the task started running
1 parent 2340bad commit 3b5970c

File tree

7 files changed

+43
-15
lines changed

7 files changed

+43
-15
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node_js:
44
- "8"
55
- "10"
66
- "12"
7+
- "14"
8+
- "15"
79
install:
810
- npm install
911
script:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
## Development
66
- nothing yet
77

8+
## v0.1.0
9+
10+
Expose if the enqueued task already started running
11+
812
## v0.0.2
913

1014
Use new version of linked-list

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ Type: `Promise<T>`
211211

212212
A promise that will be resolved with the result of `fn`.
213213

214+
###### started
215+
216+
Type: `boolean`
217+
218+
Indicates if the task has already started to run
219+
214220
##### fn
215221

216222
Type: `Function`

package.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "promise-blocking-queue",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Memory optimized promise blocking queue with concurrency control",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -54,23 +54,23 @@
5454
},
5555
"homepage": "https://github.com/PruvoNet/promise-blocking-queue#readme",
5656
"devDependencies": {
57-
"@types/chai": "^4.1.7",
58-
"@types/chai-as-promised": "^7.1.0",
59-
"@types/mocha": "^5.2.7",
60-
"@types/node": "^12.6.3",
57+
"@types/chai": "^4.2.15",
58+
"@types/chai-as-promised": "^7.1.3",
59+
"@types/mocha": "^8.2.1",
60+
"@types/node": "^14.14.31",
6161
"chai": "^4.2.0",
6262
"chai-as-promised": "^7.1.1",
63-
"coveralls": "^3.0.5",
64-
"delay": "^4.3.0",
63+
"coveralls": "^3.1.0",
64+
"delay": "^5.0.0",
6565
"dirty-chai": "^2.0.1",
6666
"istanbul": "^0.4.5",
67-
"mocha": "^6.1.4",
68-
"sinon": "^7.3.2",
69-
"sinon-chai": "^3.3.0",
70-
"sleep-promise": "^8.0.1",
71-
"ts-node": "^8.3.0",
72-
"tslint": "^5.18.0",
73-
"typescript": "^3.5.3"
67+
"mocha": "^6.2.3",
68+
"sinon": "^9.2.4",
69+
"sinon-chai": "^3.5.0",
70+
"sleep-promise": "^9.1.0",
71+
"ts-node": "^9.1.1",
72+
"tslint": "^6.1.3",
73+
"typescript": "^4.1.5"
7474
},
7575
"dependencies": {
7676
"linked-list": "^2.1.0"

src/BlockingQueue.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface IBlockingQueueEvents {
1313

1414
type MessageEmitter = StrictEventEmitter<EventEmitter, IBlockingQueueEvents>;
1515

16-
type PromiseResolve<T> = (value?: T | PromiseLike<T>) => void;
16+
type PromiseResolve<T> = (value: T | PromiseLike<T>) => void;
1717

1818
interface IPromiseParts<T> {
1919
promise: Promise<T>;
@@ -67,12 +67,15 @@ export class BlockingQueue extends (EventEmitter as new() => MessageEmitter) {
6767
fnResolve: fnPromiseParts.resolve,
6868
enqueueResolve: enqueuePromiseParts.resolve,
6969
};
70+
let started = false;
7071
if (this.activeCount < this._options.concurrency) {
72+
started = true;
7173
this._run(item);
7274
} else {
7375
this._queue.append(new Node(item));
7476
}
7577
return {
78+
started,
7679
enqueuePromise: enqueuePromiseParts.promise,
7780
fnPromise: fnPromiseParts.promise,
7881
};

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export type QueueFn<T, P extends any[]> = ((...args: P) => Promise<T> | T);
99
export interface IEnqueueResult<T> {
1010
enqueuePromise: Promise<void>;
1111
fnPromise: Promise<T>;
12+
started: boolean;
1213
}

test/src/index.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('promise blocking queue', () => {
7979
const item = queue.enqueue((x: number, y: number): string => {
8080
return `${x + y}`;
8181
}, 5, 6);
82+
expect(item.started).to.eql(true);
8283
expect(queue.activeCount).to.eql(1);
8384
expect(queue.pendingCount).to.eql(0);
8485
return item.enqueuePromise
@@ -97,6 +98,7 @@ describe('promise blocking queue', () => {
9798
const item = queue.enqueue((x: number, y: number): Promise<string> => {
9899
return Promise.resolve(`${x + y}`);
99100
}, 5, 6);
101+
expect(item.started).to.eql(true);
100102
expect(queue.activeCount).to.eql(1);
101103
expect(queue.pendingCount).to.eql(0);
102104
return item.enqueuePromise
@@ -115,6 +117,7 @@ describe('promise blocking queue', () => {
115117
const item = queue.enqueue((): string => {
116118
return `11`;
117119
});
120+
expect(item.started).to.eql(true);
118121
expect(queue.activeCount).to.eql(1);
119122
expect(queue.pendingCount).to.eql(0);
120123
return item.enqueuePromise
@@ -133,6 +136,7 @@ describe('promise blocking queue', () => {
133136
const item = queue.enqueue((): Promise<string> => {
134137
return Promise.resolve(`11`);
135138
});
139+
expect(item.started).to.eql(true);
136140
expect(queue.activeCount).to.eql(1);
137141
expect(queue.pendingCount).to.eql(0);
138142
return item.enqueuePromise
@@ -163,9 +167,11 @@ describe('promise blocking queue', () => {
163167
const events: IEvent[] = [];
164168
const queue = new BlockingQueue({concurrency: 1});
165169
const resultOne = queue.enqueue(constructRunFn(100), 'one', events);
170+
expect(resultOne.started).to.eql(true);
166171
expect(queue.activeCount).to.eql(1);
167172
expect(queue.pendingCount).to.eql(0);
168173
const resultTwo = queue.enqueue(constructRunFn(100), 'two', events);
174+
expect(resultTwo.started).to.eql(false);
169175
expect(queue.activeCount).to.eql(1);
170176
expect(queue.pendingCount).to.eql(1);
171177
return resultOne.enqueuePromise
@@ -196,12 +202,15 @@ describe('promise blocking queue', () => {
196202
const events: IEvent[] = [];
197203
const queue = new BlockingQueue({concurrency: 2});
198204
const resultOne = queue.enqueue(constructRunFn(100), 'one', events);
205+
expect(resultOne.started).to.eql(true);
199206
expect(queue.activeCount).to.eql(1);
200207
expect(queue.pendingCount).to.eql(0);
201208
const resultTwo = queue.enqueue(constructRunFn(100), 'two', events);
209+
expect(resultTwo.started).to.eql(true);
202210
expect(queue.activeCount).to.eql(2);
203211
expect(queue.pendingCount).to.eql(0);
204212
const resultThree = queue.enqueue(constructRunFn(100), 'three', events);
213+
expect(resultThree.started).to.eql(false);
205214
expect(queue.activeCount).to.eql(2);
206215
expect(queue.pendingCount).to.eql(1);
207216
return Promise.all([resultOne.enqueuePromise, resultTwo.enqueuePromise])
@@ -237,6 +246,7 @@ describe('promise blocking queue', () => {
237246
queue.enqueue(constructRunFn(200), 'one', events);
238247
queue.enqueue(constructRunFn(200), 'two', events);
239248
const resultThree = queue.enqueue(constructRunFn(0), 'three', events);
249+
expect(resultThree.started).to.eql(false);
240250
return resultThree.enqueuePromise
241251
.then(() => {
242252
return resultThree.fnPromise;
@@ -264,7 +274,9 @@ describe('promise blocking queue', () => {
264274
});
265275
queue.enqueue(constructRunFn(100), 'one', events);
266276
const resultTwo = queue.enqueue(constructRunFn(200), 'two', events);
277+
expect(resultTwo.started).to.eql(true);
267278
const resultThree = queue.enqueue(constructRunFn(200), 'three', events);
279+
expect(resultThree.started).to.eql(false);
268280
return resultTwo.fnPromise
269281
.then(() => {
270282
expect(isEmpty).to.eql(true);

0 commit comments

Comments
 (0)