@@ -9,6 +9,7 @@ class TaskPool extends events_1.EventEmitter {
99 this . DEFAULT_CONCURRENCY = 30 ;
1010 this . tasks = [ ] ;
1111 this . resolutions = [ ] ;
12+ this . errors = [ ] ;
1213 this . isRunning = false ;
1314 this . queue = [ ] ;
1415 this . running = new Set ( ) ;
@@ -28,6 +29,12 @@ class TaskPool extends events_1.EventEmitter {
2829 if ( typeof this . concurrency !== 'number' || this . concurrency < 0 ) {
2930 throw new TypeError ( 'Invalid concurrency number' ) ;
3031 }
32+ if ( typeof options ?. throwsError === 'boolean' ) {
33+ this . throwsError = options . throwsError ;
34+ }
35+ else {
36+ this . throwsError = true ;
37+ }
3138 this . on ( 'done' , this . nextTask ) ;
3239 }
3340 /**
@@ -36,19 +43,36 @@ class TaskPool extends events_1.EventEmitter {
3643 */
3744 addTask ( task ) {
3845 if ( task instanceof task_1 . Task ) {
39- this . tasks . push ( task ) ;
46+ const length = this . tasks . push ( task ) ;
47+ return length - 1 ;
4048 }
41- else if ( task instanceof Array ) {
42- task . forEach ( ( t ) => {
43- if ( ! ( t instanceof task_1 . Task ) ) {
44- throw new TypeError ( 'Invalid task' ) ;
45- }
46- this . tasks . push ( t ) ;
47- } ) ;
49+ // throw error if params is not a task or a tasks array.
50+ throw new TypeError ( 'Invalid task(s)' ) ;
51+ }
52+ /**
53+ * Add tasks array into pool.
54+ * @param tasks Task instances array.
55+ */
56+ addTasks ( tasks ) {
57+ let taskArr ;
58+ if ( tasks instanceof Array ) {
59+ taskArr = tasks ;
60+ }
61+ else if ( tasks instanceof task_1 . Task ) {
62+ taskArr = [ tasks ] ;
4863 }
4964 else {
50- throw new TypeError ( 'Invalid task' ) ;
65+ throw new TypeError ( 'Invalid task(s) ' ) ;
5166 }
67+ const ids = [ ] ;
68+ taskArr . forEach ( ( t ) => {
69+ if ( ! ( t instanceof task_1 . Task ) ) {
70+ throw new TypeError ( 'Invalid task' ) ;
71+ }
72+ const length = this . tasks . push ( t ) ;
73+ ids . push ( length - 1 ) ;
74+ } ) ;
75+ return ids ;
5276 }
5377 /**
5478 * Execute all tasks in the pool.
@@ -71,19 +95,37 @@ class TaskPool extends events_1.EventEmitter {
7195 }
7296 this . concurrency = concurrency ;
7397 }
98+ /**
99+ * Gets execution errors of last time.
100+ */
101+ getErrors ( ) {
102+ return [ ...this . errors ] ;
103+ }
104+ /**
105+ * Get task instance by it's id.
106+ */
107+ getTask ( id ) {
108+ return id in this . tasks ? this . tasks [ id ] : null ;
109+ }
74110 runTask ( index ) {
75111 if ( index >= this . tasks . length ) {
76112 throw new Error ( 'Invalid task' ) ;
77113 }
78114 const task = this . tasks [ index ] ;
79115 this . running . add ( index ) ;
80- Promise . resolve ( task . exec ( ) ) . then ( ( res ) => {
81- this . running . delete ( index ) ;
82- this . resolutions [ index ] = res ;
83- this . emit ( 'done' ) ;
84- } , ( err ) => {
85- this . emit ( 'error' , err ) ;
86- } ) ;
116+ try {
117+ Promise . resolve ( task . exec ( ) ) . then ( ( res ) => {
118+ this . running . delete ( index ) ;
119+ this . resolutions [ index ] = res ;
120+ this . emit ( 'done' ) ;
121+ } , ( err ) => {
122+ this . running . delete ( index ) ;
123+ this . handleError ( err , index ) ;
124+ } ) ;
125+ }
126+ catch ( err ) {
127+ this . handleError ( err , index ) ;
128+ }
87129 }
88130 nextTask ( ) {
89131 const next = this . queue . shift ( ) ;
@@ -100,9 +142,13 @@ class TaskPool extends events_1.EventEmitter {
100142 this . running . clear ( ) ;
101143 this . queue = [ ] ;
102144 this . isRunning = true ;
145+ // clear last resolutions and errors
146+ this . resolutions = [ ] ;
147+ this . errors = [ ] ;
103148 // eslint-disable-next-line no-unused-vars
104149 return new Promise ( ( resolve , reject ) => {
105150 this . on ( 'error' , ( err ) => {
151+ this . isRunning = false ;
106152 reject ( err ) ;
107153 } ) ;
108154 this . on ( 'completed' , ( ) => {
@@ -124,5 +170,16 @@ class TaskPool extends events_1.EventEmitter {
124170 throw new Error ( 'Task pool is executing' ) ;
125171 }
126172 }
173+ handleError ( err , index ) {
174+ if ( this . throwsError ) {
175+ this . queue = [ ] ;
176+ this . emit ( 'error' , err ) ;
177+ }
178+ else {
179+ this . running . delete ( index ) ;
180+ this . errors [ index ] = err ;
181+ this . emit ( 'done' ) ;
182+ }
183+ }
127184}
128185exports . TaskPool = TaskPool ;
0 commit comments