import { DelayedRunner } from './DelayedRunner' ; export class TaskRunner < Task > { private queue : Task [ ] = [ ] ; private delayedRunner : DelayedRunner ; constructor ( private runTaskOption ? , private drainedOption ? ) { this . delayedRunner = new DelayedRunner ( this . drain . bind ( this ) ) ; } request ( task , delay ? ) { this . queue . push ( task ) ; this . delayedRunner . request ( delay ) ; } pause ( scope ? ) { this . delayedRunner . pause ( scope ) ; } resume ( scope ? , force ? ) { this . delayedRunner . resume ( scope , force ) ; } drain ( ) { let { queue } = this ; while ( queue . length ) { let completedTasks = [ ] ; let task ; while ( ( task = queue . shift ( ) ) ) { this . runTask ( task ) ; completedTasks . push ( task ) ; } this . drained ( completedTasks ) ; } } protected runTask ( task ) { if ( this . runTaskOption ) { this . runTaskOption ( task ) ; } } protected drained ( completedTasks ) { if ( this . drainedOption ) { this . drainedOption ( completedTasks ) ; } } }