export type AsyncTaskSchedulerFinalizeCallback < T > = ( ids : readonly T [ ] ) => void ; export class AsyncTaskScheduler < T > { private readonly aborters : Set < ( ) => void > = new Set ( ) ; private readonly finalizeCallback : AsyncTaskSchedulerFinalizeCallback < T > ; private readonly tasks : Map < T , ( ) => void > = new Map ( ) ; public constructor ( finalizeCallback ) { this . finalizeCallback = finalizeCallback ; } public add ( id , task , aborter ) { this . tasks . set ( id , task ) ; this . dropOthers ( ) ; this . aborters . add ( aborter ) ; } public finalize ( ) { this . tasks . forEach ( ( task ) => task ( ) ) ; this . finalizeCallback ( Array . from ( this . tasks . keys ( ) ) ) ; } private dropOthers ( ) { this . aborters . forEach ( ( aborter ) => aborter ( ) ) ; this . aborters . clear ( ) ; } }