export type Callback = ( err : Error | null , result ? : any ) => void ; interface Task < T > { item : T ; callback ? : Callback ; } type Worker < T > = ( item : T , cb : Callback ) => void ; export class Queue < T = unknown > { private _worker : Worker < T > ; private _concurrency : number ; tasks : Task < T > [ ] ; total : number ; active : number ; param { Function } worker param options param { ! number } options . concurrency constructor ( worker , options = { } ) { this . _worker = worker ; this . _concurrency = options . concurrency || 1 ; this . tasks = [ ] ; this . total = 0 ; this . active = 0 ; } param { T } item param { ! Function } callback push ( item , callback ? ) { this . tasks . push ( { item , callback } ) ; this . total ++ ; this . _next ( ) ; } /**
     * Process next job in queue.
     */ _next ( ) { if ( this . active >= this . _concurrency || ! this . tasks . length ) { return ; } const { item , callback } = this . tasks . shift ( ) as Task < T > ; let callbackCalled = false ; this . active ++ ; this . _worker ( item , ( err , result ) => { if ( callbackCalled ) { return ; } this . active -- ; callbackCalled = true ; callback ?. ( err , result ) ; this . _next ( ) ; } ) ; } /**
     * Stops processing queued jobs.
     */ die ( ) { this . tasks = [ ] ; } }