import getGuid from './tools.guid' ; import Logger from './env.logger' ; export class PromisesQueue { private _tasks : Map < string , Promise < any > > = new Map ( ) ; private _pending : Array < ( ) => any > = [ ] ; private _logger : Logger ; constructor ( alias ) { this . _logger = new Logger ( alias ) ; } public add ( task , guid ? ) { const id = typeof guid !== 'string' ? getGuid ( ) : guid ; this . _tasks . set ( id , task ) ; task . catch ( ( ) => { this . _remove ( id ) ; } ) . then ( ( ) => { this . _remove ( id ) ; } ) ; return task ; } public do ( callback ) { if ( this . _tasks . size > 0 ) { this . _logger . debug ( `Cannot start callback because still have a tasks in queue (count - ${ this . _tasks . size });` ) ; this . _pending . push ( callback ) ; } else { callback ( ) ; } } public has ( guid ) { return this . _tasks . has ( guid ) ; } private _remove ( guid ) { this . _tasks . delete ( guid ) ; if ( this . _tasks . size === 0 ) { this . _pending . forEach ( ( callback ) => { callback ( ) ; } ) ; this . _pending = [ ] ; } } }