import { Disposable } from '@most/types' ; import { disposeOnce } from '@most/disposable' ; export { Disposable } from '@most/types' ; name type export interface Future < A , B > { readonly fork : Fork < A , B > ; } name type export type Fork < A , B > = ( reject : ( value : A ) => void , resolve : ( value : B ) => void ) => Disposable ; export namespace Future { name export function create < A , B > ( fork ) { return { fork : ( reject , resolve ) : Disposable => { let settled = false ; function isUnsettled ( ) { if ( settled ) return false ; settled = true ; return true ; } const disposable = disposeOnce ( fork ( value => isUnsettled ( ) && reject ( value ) , value => isUnsettled ( ) && resolve ( value ) ) ) ; const dispose = ( ) => disposable . dispose ( ) ; return { dispose } ; } , } ; } name export function of < A , B = any > ( value ) { return create ( ( _ , resolve ) => defer ( resolve , value ) ) ; } name export function reject < A , B = any > ( value ) { return create ( reject => defer ( reject , value ) ) ; } } function defer < A > ( f , value ) { const id = setTimeout ( f , 0 , value ) ; const dispose = ( ) => clearTimeout ( id ) ; return { dispose } ; }