Skip to content

Commit 80548d1

Browse files
committed
update
1 parent eaff151 commit 80548d1

10 files changed

+3302
-673
lines changed

.editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
coverage

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ pq.add(() => {
7676
});
7777

7878
// syntax sugar:
79-
pq.add([promiseThunk, promiseThunk, promiseThunk]);
80-
// is equal to:
8179
pq.add(promiseThunk).add(promiseThunk).add(promiseThunk);
8280
// is equal to:
8381
pq.add(promiseThunk);

README_CN.md

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ pq.add(() => {
7575
});
7676
7777
// syntax sugar:
78-
pq.add([promiseThunk, promiseThunk, promiseThunk]);
79-
// is equal to:
8078
pq.add(promiseThunk).add(promiseThunk).add(promiseThunk);
8179
// is equal to:
8280
pq.add(promiseThunk);

jest.config.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
module.exports = {
3+
collectCoverage: true,
4+
collectCoverageFrom:
5+
[
6+
'src/**/*.ts'
7+
],
8+
coveragePathIgnorePatterns:
9+
[
10+
'/coverage/',
11+
'/node_modules/',
12+
'.config.js'
13+
],
14+
coverageDirectory: './coverage',
15+
moduleFileExtensions: [
16+
"ts",
17+
"tsx",
18+
"js"
19+
],
20+
transform: {
21+
"^.+\\.(ts|tsx)$":
22+
"ts-jest"
23+
},
24+
globals: {
25+
"ts-jest": {
26+
"tsConfig": "tsconfig.json"
27+
}
28+
},
29+
testMatch: [
30+
"**/test/*.+(ts|tsx|js)"
31+
]
32+
};

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"waterfall"
2424
],
2525
"devDependencies": {
26+
"@types/jest": "^23.3.10",
27+
"@types/node": "^10.12.15",
28+
"jest": "^23.6.0",
29+
"ts-jest": "^23.10.5",
2630
"tslint": "^5.11.0",
2731
"typescript": "^3.2.2"
2832
},

src/PromiseQueue.ts

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as utils from "./typeCheck";
2-
31
interface IPromiseQueueOpts {
42
concurrency: number;
53
}
@@ -34,35 +32,29 @@ class PromiseQueue {
3432
this._next();
3533
}
3634

37-
public add(fn: () => Promise<any> | Array<() => Promise<any>>): PromiseQueue | TypeError {
38-
if (utils.isArray(fn) && fn.every(utils.isFunction)) {
39-
return fn.length > 1 ? this.add(fn.shift()).add(fn) : this.add(fn[0]);
40-
} else if (utils.isFunction(fn)) {
41-
new Promise((resolve, reject) => {
42-
const run = () => {
43-
this._ongoingCount++;
44-
fn().then(
45-
(val: any) => {
46-
resolve(val);
47-
this._next();
48-
},
49-
(err: Error) => {
50-
reject(err);
51-
this._next();
52-
}
53-
);
54-
};
55-
56-
if (this._ongoingCount < this._concurrency && !this._pause) {
57-
run();
58-
} else {
59-
this._queue.push(run);
60-
}
61-
});
62-
return this;
63-
} else {
64-
throw new TypeError('Expected `arg` in add(arg) must be a function which return a Promise, or an array of function which return a Promise');
65-
}
35+
public add(fn: () => Promise<any>): PromiseQueue | TypeError {
36+
new Promise((resolve, reject) => {
37+
const run = () => {
38+
this._ongoingCount++;
39+
fn().then(
40+
(val: any) => {
41+
resolve(val);
42+
this._next();
43+
},
44+
(err: Error) => {
45+
reject(err);
46+
this._next();
47+
}
48+
);
49+
};
50+
51+
if (this._ongoingCount < this._concurrency && !this._pause) {
52+
run();
53+
} else {
54+
this._queue.push(run);
55+
}
56+
});
57+
return this;
6658
}
6759

6860
// Promises which are not ready yet to run in the queue.

src/typeCheck.ts

-7
This file was deleted.

test/index.js test/PromiseQueue.test.ts

+25-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('When the concurrency limit is 1', function () {
66
const promiseQueue = new PromiseQueue({concurrency: 1});
77
promiseQueue.add(() => {
88
return new Promise(resolve => {
9-
assert.equal(promiseQueue.ongoingCount, 1);
9+
assert.strictEqual(promiseQueue.ongoingCount, 1);
1010
setTimeout(function () {
1111
resolve(1);
1212
}, 500)
@@ -17,7 +17,7 @@ describe('When the concurrency limit is 1', function () {
1717
return new Promise(resolve => {
1818
setTimeout(function () {
1919
resolve(1);
20-
assert.equal(promiseQueue.ongoingCount, 1);
20+
assert.strictEqual(promiseQueue.ongoingCount, 1);
2121
}, 500)
2222
})
2323
});
@@ -26,17 +26,17 @@ describe('When the concurrency limit is 1', function () {
2626
return new Promise(resolve => {
2727
setTimeout(function () {
2828
resolve(1);
29-
assert.equal(promiseQueue.ongoingCount, 1);
29+
assert.strictEqual(promiseQueue.ongoingCount, 1);
3030
done();
3131
}, 500)
3232
})
3333
});
3434

3535
// only one promise is waiting to run
36-
assert.equal(promiseQueue.waitingCount, 2);
36+
assert.strictEqual(promiseQueue.waitingCount, 2);
3737

3838
// only one promise is running
39-
assert.equal(promiseQueue.ongoingCount, 1);
39+
assert.strictEqual(promiseQueue.ongoingCount, 1);
4040
});
4141
});
4242

@@ -47,7 +47,7 @@ describe('When the concurrency limit is 2', function () {
4747
return new Promise(resolve => {
4848
setTimeout(function () {
4949
resolve(1);
50-
assert.equal(promiseQueue.ongoingCount, 2);
50+
assert.strictEqual(promiseQueue.ongoingCount, 2);
5151
}, 500)
5252
})
5353
});
@@ -56,7 +56,7 @@ describe('When the concurrency limit is 2', function () {
5656
return new Promise(resolve => {
5757
setTimeout(function () {
5858
resolve(1);
59-
assert.equal(promiseQueue.ongoingCount, 2);
59+
assert.strictEqual(promiseQueue.ongoingCount, 2);
6060
}, 500)
6161
})
6262
});
@@ -65,7 +65,7 @@ describe('When the concurrency limit is 2', function () {
6565
return new Promise(resolve => {
6666
setTimeout(function () {
6767
resolve(1);
68-
assert.equal(promiseQueue.ongoingCount, 2);
68+
assert.strictEqual(promiseQueue.ongoingCount, 2);
6969
}, 500)
7070
})
7171
});
@@ -81,10 +81,10 @@ describe('When the concurrency limit is 2', function () {
8181
});
8282

8383
// only two promise is waiting to run
84-
assert.equal(promiseQueue.waitingCount, 2);
84+
assert.strictEqual(promiseQueue.waitingCount, 2);
8585

8686
// only two promises is running
87-
assert.equal(promiseQueue.ongoingCount, 2);
87+
assert.strictEqual(promiseQueue.ongoingCount, 2);
8888
});
8989
});
9090

@@ -93,7 +93,7 @@ describe('"Add" method can be chaining', function () {
9393
const promiseQueue = new PromiseQueue({concurrency: 1});
9494
let pqInstance = promiseQueue.add(() => {
9595
return new Promise(resolve => {
96-
assert.equal(promiseQueue.ongoingCount, 1);
96+
assert.strictEqual(promiseQueue.ongoingCount, 1);
9797
setTimeout(function () {
9898
resolve(1);
9999
}, 500)
@@ -102,20 +102,23 @@ describe('"Add" method can be chaining', function () {
102102

103103
assert.ok(pqInstance instanceof PromiseQueue);
104104

105-
pqInstance.add(() => {
106-
return new Promise(resolve => {
107-
setTimeout(function () {
108-
resolve(1);
109-
assert.equal(promiseQueue.ongoingCount, 1);
110-
done();
111-
}, 500)
112-
})
113-
});
105+
if (!(pqInstance instanceof TypeError)) {
106+
pqInstance.add(() => {
107+
return new Promise(resolve => {
108+
setTimeout(function () {
109+
resolve(1);
110+
assert.strictEqual(promiseQueue.ongoingCount, 1);
111+
done();
112+
}, 500)
113+
})
114+
});
115+
}
114116

115117
// only one promise is waiting to run
116-
assert.equal(promiseQueue.waitingCount, 1);
118+
assert.strictEqual(promiseQueue.waitingCount, 1);
117119

118120
// only one promise is running
119-
assert.equal(promiseQueue.ongoingCount, 1);
121+
assert.strictEqual(promiseQueue.ongoingCount, 1);
120122
});
123+
121124
});

0 commit comments

Comments
 (0)