import { PromiseFunctions } from "../promise_functions" ; import { List } from "./list" ; /**
 * FIFO queue for `Promise`s. Supports pushing single elements onto the queue
 * and settling one or all elements in the queue. Implemented using `List`.
 */ export class PromiseQueue < T > { private queue : List < PromiseFunctions < T > > = new List < PromiseFunctions < T > > ( ) ; returns public constructor ( ) { } returns public async push ( ) { const promise = new Promise < T > ( ( resolve , reject ) => { this . queue . push ( { reject , resolve } ) ; } ) ; return promise ; } param value returns public resolveAll ( value ) { return invokeWhile ( ( ) => this . resolveOne ( value ) ) ; } param reason returns public rejectAll ( reason ? ) { return invokeWhile ( ( ) => this . rejectOne ( reason ) ) ; } param value returns public resolveOne ( value ) { return this . settleOne ( "resolve" , value ) ; } param reason returns public rejectOne ( reason ? ) { return this . settleOne ( "reject" , reason ) ; } param method param argument returns private settleOne ( method , argument ? ) { const maybeHead = this . queue . shift ( ) ; if ( typeof maybeHead === "undefined" ) { return false ; } const toSettle = maybeHead ; if ( method === "reject" ) { toSettle . reject ( argument ) ; } else if ( method === "resolve" ) { toSettle . resolve ( argument ) ; } return true ; } } param invocable returns const invokeWhile = ( condition ) : number => { let numTrue = 0 ; while ( condition ( ) ) { ++ numTrue ; } return numTrue ; } ;