export class PromiseBarrier < RESOLVUTION , REJECTION > { private resolves : Array < ( value ? : RESOLVUTION ) => void > = [ ] ; private rejects : Array < ( reason ? : REJECTION ) => void > = [ ] ; private wasResolved = false ; private wasRejected = false ; private resolution : RESOLVUTION ; private reason : REJECTION ; async entry ( ) { if ( this . wasResolved ) { return Promise . resolve ( this . resolution ) ; } if ( this . wasRejected ) { return Promise . reject ( this . reason ) ; } return new Promise ( ( resolve , reject ) => { this . resolves . push ( resolve ) ; this . rejects . push ( reject ) ; } ) ; } async resolve ( value ? ) { if ( this . wasRejected || this . wasResolved ) { return ; } this . wasResolved = true ; this . resolution = value ; this . resolves . forEach ( ( resolve ) => resolve ( value ) ) ; this . resolves = null ; this . rejects = null ; return new Promise < void > ( ( resolve ) => setTimeout ( ( ) => resolve ( ) ) ) ; } async reject ( reason ? ) { if ( this . wasRejected || this . wasResolved ) { return ; } this . wasRejected = true ; this . reason = reason ; this . rejects . forEach ( ( reject ) => reject ( reason ) ) ; this . resolves = null ; this . rejects = null ; return new Promise < void > ( ( resolve ) => setTimeout ( ( ) => resolve ( ) ) ) ; } isPending ( ) { return ! this . wasResolved && ! this . wasRejected ; } isFulfilled ( ) { return this . wasResolved ; } isRejected ( ) { return this . wasRejected ; } }