@@ -9,7 +9,7 @@ const util = require('util');
99 *
1010 * @param {Number } opts.concurrency - number of concurrent process, default is 10
1111 * @param {number } opts.workMS - work-time millseconds at once, default is 0 (no wait)
12- * @param {Function } workPromise - work-function returns a Promise callwed with an item `function (item)`
12+ * @param {Promise|[Promise] } workPromise - work-function returns a Promise callwed with an item `function (item)`
1313 */
1414function PacedWorkStream ( opts , workPromise ) {
1515 if ( ! ( this instanceof PacedWorkStream ) )
@@ -23,6 +23,7 @@ function PacedWorkStream(opts, workPromise) {
2323
2424 this . tagCounts = { } ;
2525 this . _bufItems = [ ] ;
26+ this . _bufPrmss = [ ] ;
2627 if ( workPromise ) {
2728 this . _workPromise = workPromise ;
2829 }
@@ -35,37 +36,64 @@ util.inherits(PacedWorkStream, Transform);
3536PacedWorkStream . prototype . _transform = function ( data , encoding , cb ) {
3637 this . _bufItems . push ( data ) ;
3738
39+ if ( this . _bufPrmss . length >= this . _concurrency ) {
40+ const doPromises = this . _bufPrmss . splice ( 0 , this . _concurrency ) ;
41+ this . _process ( doPromises , cb ) ;
42+ return ;
43+ }
44+
3845 if ( this . _bufItems . length < this . _concurrency ) {
3946 cb ( ) ;
4047 return ;
4148 }
4249
4350 const items = this . _bufItems . splice ( 0 , this . _concurrency ) ;
44- this . _process ( items , cb ) ;
51+ try {
52+ this . _bufPrmss = this . _bufPrmss . concat ( this . _toPromises ( items ) ) ;
53+ const doPromises = this . _bufPrmss . splice ( 0 , this . _concurrency ) ;
54+ this . _process ( doPromises , cb ) ;
55+ } catch ( err ) {
56+ cb ( err ) ;
57+ }
4558}
4659PacedWorkStream . prototype . _flush = function ( cb ) {
47- if ( this . _bufItems . length ) {
48- this . _process ( this . _bufItems , ( err ) => {
60+ const restItems = this . _bufItems ;
61+ if ( restItems . length ) {
62+ try {
63+ this . _bufPrmss = this . _bufPrmss . concat ( this . _toPromises ( restItems ) ) ;
64+ } catch ( err ) {
4965 cb ( err ) ;
50- this . emit ( 'done' ) ;
51- } ) ;
52- this . _bufItems = [ ] ;
53- } else {
54- cb ( ) ;
55- this . emit ( 'done' ) ;
66+ return ;
67+ }
5668 }
69+
70+ this . _flushPromises ( cb ) ;
5771}
5872
59- PacedWorkStream . prototype . _process = function ( items , cb ) {
60- let promises ;
61- try {
62- promises = items . map ( ( item ) => {
63- return this . _workPromise ( item ) ;
64- } ) ;
65- } catch ( err ) {
66- return cb ( err ) ;
73+ PacedWorkStream . prototype . _flushPromises = function ( cb ) {
74+ if ( this . _bufPrmss . length === 0 ) {
75+ cb ( ) ;
76+ this . emit ( 'done' ) ;
77+ return ;
6778 }
6879
80+ const doPromises = this . _bufPrmss . splice ( 0 , this . _concurrency ) ;
81+ this . _process ( doPromises , ( err ) => {
82+ if ( err ) {
83+ cb ( err ) ;
84+ } else {
85+ this . _flushPromises ( cb ) ;
86+ }
87+ } ) ;
88+ }
89+ PacedWorkStream . prototype . _toPromises = function ( items ) {
90+ let promises = [ ] ;
91+ items . forEach ( ( item ) => {
92+ promises = promises . concat ( this . _workPromise ( item ) ) ;
93+ } ) ;
94+ return promises ;
95+ }
96+ PacedWorkStream . prototype . _process = function ( promises , cb ) {
6997 const startTime = new Date ( ) . getTime ( ) ;
7098 Promise . all ( promises )
7199 . then ( ( results ) => {
0 commit comments