1
1
import DebugLogger from "debug"
2
2
import { multicast , Observable , Subject } from "observable-fns"
3
- import { allSettled , SettlementResult } from "../ponyfills"
3
+ import { allSettled } from "../ponyfills"
4
4
import { defaultPoolSize } from "./implementation"
5
5
import {
6
6
PoolEvent ,
@@ -326,11 +326,17 @@ class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
326
326
throw this . initErrors [ 0 ]
327
327
}
328
328
329
- const taskCompleted = ( ) => this . taskCompletion ( task . id )
330
- let taskCompletionDotThen : Promise < any > [ "then" ] | undefined
329
+ const taskID = this . nextTaskID ++
330
+ const taskCompletion = this . taskCompletion ( taskID )
331
+
332
+ taskCompletion . catch ( ( error ) => {
333
+ // Prevent unhandled rejections here as we assume the user will use
334
+ // `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors
335
+ this . debug ( `Task #${ taskID } errored:` , error )
336
+ } )
331
337
332
338
const task : QueuedTask < ThreadType , any > = {
333
- id : this . nextTaskID ++ ,
339
+ id : taskID ,
334
340
run : taskFunction ,
335
341
cancel : ( ) => {
336
342
if ( this . taskQueue . indexOf ( task ) === - 1 ) return
@@ -340,13 +346,7 @@ class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
340
346
taskID : task . id
341
347
} )
342
348
} ,
343
- get then ( ) {
344
- if ( ! taskCompletionDotThen ) {
345
- const promise = taskCompleted ( )
346
- taskCompletionDotThen = promise . then . bind ( promise )
347
- }
348
- return taskCompletionDotThen
349
- }
349
+ then : taskCompletion . then . bind ( taskCompletion )
350
350
}
351
351
352
352
if ( this . taskQueue . length >= maxQueuedJobs ) {
0 commit comments