license import { assert } from '../util' ; import { OnProgressCallback } from './types' ; param promises param onProgress param startFraction param endFraction export function monitorPromisesProgress ( promises , onProgress , startFraction ? , endFraction ? ) { checkPromises ( promises ) ; startFraction = startFraction == null ? 0 : startFraction ; endFraction = endFraction == null ? 1 : endFraction ; checkFraction ( startFraction , endFraction ) ; let resolvedPromise = 0 ; const registerMonitor = ( promise ) => { promise . then ( value => { const fraction = startFraction + ++ resolvedPromise / promises . length * ( endFraction - startFraction ) ; onProgress ( fraction ) ; return value ; } ) ; return promise ; } ; function checkPromises ( promises ) { assert ( promises != null && Array . isArray ( promises ) && promises . length > 0 , ( ) => 'promises must be a none empty array' ) ; } function checkFraction ( startFraction , endFraction ) { assert ( startFraction >= 0 && startFraction <= 1 , ( ) => `Progress fraction must be in range [0, 1], but ` + `got startFraction ${ startFraction }` ) ; assert ( endFraction >= 0 && endFraction <= 1 , ( ) => `Progress fraction must be in range [0, 1], but ` + `got endFraction ${ endFraction }` ) ; assert ( endFraction >= startFraction , ( ) => `startFraction must be no more than endFraction, but ` + `got startFraction ${ startFraction } and endFraction ` + `${ endFraction }` ) ; } return Promise . all ( promises . map ( registerMonitor ) ) ; }