import { Disposable , Future } from './Future' ; import { append , reduce } from '@typed/list' ; import { disposeAll } from '@most/disposable' ; import { fork } from './fork' ; name export function all < A , B > ( futures ) { const { length : futureCount } = futures ; const values = Array ( futureCount ) ; const remaining = { count : futureCount } ; return Future . create < A , ReadonlyArray < B > > ( ( reject , resolve ) => disposeAll ( reduce ( forkFuture < A , B > ( reject , resolve , values , remaining ) , [ ] , futures ) ) ) ; } type Remaining = { count : number ; } ; function forkFuture < A , B > ( reject , resolve , values , remaining ) { return ( disposables , future , index ) : Array < Disposable > => append ( fork ( reject , value => { values [ index ] = value ; remaining . count -- ; if ( remaining . count === 0 ) resolve ( values ) ; } , future ) , disposables ) ; }