import { useState , useMemo } from 'react' ; import { Observable } from 'rxjs/internal/Observable' ; import { Subject } from 'rxjs/internal/Subject' ; import { Http , http } from './http' ; import { useRxEffect } from './use-rx-effect' ; function useReqRes < Req , Res > ( api$ ) { const req$ = new Subject < Req > ( ) ; const res$ = req$ . pipe ( http ( api$ ) ) ; return [ res$ , req$ . next . bind ( req$ ) ] ; } param api$ param next export function useRxAjax < Req , Res > ( api$ , next ? ) { const [ res$ , submitReq ] = useMemo ( ( ) => useReqRes ( api$ ) , [ api$ ] ) ; const [ res , setRes ] = useState < Http < Req , Res > > ( ) ; useRxEffect ( res$ , ( nextRes ) => { setRes ( nextRes ) ; if ( next ) { next ( nextRes ) ; } } ) ; return [ res , submitReq ] ; }