import { Disposable , withIsDisposed } from '@typed/disposable' ; import { Future } from '@typed/future' ; export function futurify < R > ( fn ) ; export function futurify < A , R > ( fn ) ; export function futurify < A , B , R > ( fn ) ; export function futurify < A , B , C , R > ( fn ) ; export function futurify < A , B , C , D , R > ( fn ) ; export function futurify < A , B , C , D , E , R > ( fn ) ; export function futurify < A , B , C , D , E , F , R > ( fn ) ; export function futurify < A , B , C , D , E , F , G , R > ( fn ) ; export function futurify < A extends NodeCallbackFn > ( fn ) { return ( ... args ) : Future < never , Error , any > => { return Future . create < never , Error , any > ( ( reject , resolve ) => { const disposable = Disposable . lazy ( ) ; disposable . addDisposable ( withIsDisposed ( ( isDisposed ) => { args . push ( ( err , value ? ) => { if ( isDisposed ( ) ) { return ; } if ( err ) { return disposable . addDisposable ( reject ( err ) ) ; } return disposable . addDisposable ( resolve ( value ! ) ) ; } ) ; fn . apply ( void 0 , args as any ) ; } ) ) ; return disposable ; } ) ; } ; } export type NodeCallback < A > = ( error : Error | null | undefined | void , value ? : A ) => void ; export type NodeCallbackFn = { < A , B , C , D , E , F , G , R > ( a , b , c , d , e , f , g , cb ) : void ; < A , B , C , D , E , F , R > ( a , b , c , d , e , f , cb ) : void ; < A , B , C , D , E , R > ( a , b , c , d , e , cb ) : void ; < A , B , C , D , R > ( a , b , c , d , cb ) : void ; < A , B , C , R > ( a , b , c , cb ) : void ; < A , B , R > ( a , b , cb ) : void ; < A , R > ( a , cb ) : void ; < R > ( cb ) : void ; } ;