import { SearchResponse } from 'elasticsearch' ; import { FetchHandlers , SearchRequest } from '../fetch' ; import { defaultSearchStrategy } from './default_search_strategy' ; import { ISearchOptions } from '../../index' ; export function callClient ( searchRequests , requestsOptions = [ ] , fetchHandlers ) { const requestOptionEntries = searchRequests . map ( ( request , i ) => [ request , requestsOptions [ i ] ] ) ; const requestOptionsMap = new Map < SearchRequest , ISearchOptions > ( requestOptionEntries ) ; const requestResponseMap = new Map < SearchRequest , Promise < SearchResponse < any > > > ( ) ; const { searching , abort } = defaultSearchStrategy . search ( { searchRequests , ... fetchHandlers , } ) ; searchRequests . forEach ( ( request , i ) => { const response = searching . then ( ( results ) => fetchHandlers . onResponse ( request , results [ i ] ) ) ; const { abortSignal = null } = requestOptionsMap . get ( request ) || { } ; if ( abortSignal ) abortSignal . addEventListener ( 'abort' , abort ) ; requestResponseMap . set ( request , response ) ; } ) ; return searchRequests . map ( ( request ) => requestResponseMap . get ( request ) ! ) ; }