import { SearchResponse } from 'elasticsearch' ; import { UI_SETTINGS } from '../../../constants' ; import { FetchHandlers , SearchRequest } from '../fetch' ; import { ISearchOptions } from '../../index' ; import { callClient } from './call_client' ; /**
 * This function introduces a slight delay in the request process to allow multiple requests to queue
 * up (e.g. when a dashboard is loading).
 */ export async function fetchSoon ( request , options , fetchHandlers ) { const msToDelay = fetchHandlers . getConfig ( UI_SETTINGS . COURIER_BATCH_SEARCHES ) ? 50 : 0 ; return delayedFetch ( request , options , fetchHandlers , msToDelay ) ; } param fn param ms return function delay < T > ( fn , ms ) { return new Promise ( ( resolve ) => { setTimeout ( ( ) => resolve ( fn ( ) ) , ms ) ; } ) ; } let requestsToFetch = [ ] ; let requestOptions = [ ] ; let fetchInProgress = null ; param request param ms return async function delayedFetch ( request , options , fetchHandlers , ms ) { if ( ms === 0 ) { return callClient ( [ request ] , [ options ] , fetchHandlers ) [ 0 ] ; } const i = requestsToFetch . length ; requestsToFetch = [ ... requestsToFetch , request ] ; requestOptions = [ ... requestOptions , options ] ; const responses = await ( fetchInProgress = fetchInProgress || delay ( ( ) => { const response = callClient ( requestsToFetch , requestOptions , fetchHandlers ) ; requestsToFetch = [ ] ; requestOptions = [ ] ; fetchInProgress = null ; return response ; } , ms ) ) ; return responses [ i ] ; }