@@ -51,21 +51,20 @@ export type WorkerpoolOptions<TPayload = JsonValue, TResult = unknown> = {
5151 dequeue : ( ) => Promisable < Task < TPayload > | undefined > ;
5252
5353 /**
54- * Called when a dequeued task is successful, use this function to remove
55- * finished tasks (mutex).
54+ * Callback style task handler.
5655 */
57- success ?: (
58- result : TResult ,
59- context : CallbackContext < TPayload , TResult >
60- ) => Promisable < void > ;
61-
62- /**
63- * Called when a failing task has exceeded maximum retries.
64- */
65- failure ?: (
66- error : Error ,
67- context : CallbackContext < TPayload , TResult >
68- ) => Promisable < void > ;
56+ onTaskFinished ?: {
57+ (
58+ error : Error ,
59+ result : null ,
60+ context : CallbackContext < TPayload , TResult >
61+ ) : Promisable < void > ;
62+ (
63+ error : null ,
64+ result : TResult ,
65+ context : CallbackContext < TPayload , TResult >
66+ ) : Promisable < void > ;
67+ } ;
6968
7069 /**
7170 * Called when the state of the pool is changed.
@@ -127,7 +126,7 @@ export class Workerpool<TPayload = JsonValue, TResult = unknown> {
127126
128127 start ( ) {
129128 if ( this . #active) {
130- return ;
129+ return this ;
131130 }
132131
133132 this . #active = true ;
@@ -221,16 +220,17 @@ export class Workerpool<TPayload = JsonValue, TResult = unknown> {
221220 runner
222221 . execute ( task . payload )
223222 . then (
224- ( result ) => this . options . success ?.( result , { task, runner } ) ,
223+ ( result ) =>
224+ this . options . onTaskFinished ?.( null , result , { task, runner } ) ,
225225 ( error ) => {
226226 if (
227227 error instanceof RunnerExecutionError &&
228228 error . retryable &&
229229 task . executionCount < this . #maximumRetries
230230 ) {
231231 this . enqueue ( task ) ;
232- } else if ( this . options . failure ) {
233- return this . options . failure ( error , { task, runner } ) ;
232+ } else if ( this . options . onTaskFinished ) {
233+ return this . options . onTaskFinished ( error , null , { task, runner } ) ;
234234 } else {
235235 throw error ;
236236 }
0 commit comments