diff --git a/examples/embeddable_examples/public/react_embeddables/search/get_count.ts b/examples/embeddable_examples/public/react_embeddables/search/get_count.ts index d4cdb2e70cb33..fdb1269097c00 100644 --- a/examples/embeddable_examples/public/react_embeddables/search/get_count.ts +++ b/examples/embeddable_examples/public/react_embeddables/search/get_count.ts @@ -16,7 +16,9 @@ export async function getCount( dataService: DataPublicPluginStart, filters: Filter[], query: Query | AggregateQuery | undefined, - timeRange: TimeRange | undefined + timeRange: TimeRange | undefined, + abortSignal: AbortSignal, + sessionId?: string ) { const searchSource = await dataService.search.searchSource.create(); searchSource.setField('index', dataView); @@ -38,7 +40,9 @@ export async function getCount( const { rawResponse: resp } = await lastValueFrom( searchSource.fetch$({ + abortSignal, legacyHitsTotal: false, + sessionId, }) ); // eslint-disable-next-line no-console diff --git a/examples/embeddable_examples/public/react_embeddables/search/search_react_embeddable.tsx b/examples/embeddable_examples/public/react_embeddables/search/search_react_embeddable.tsx index 6d5aff1dcb490..3f1918beda531 100644 --- a/examples/embeddable_examples/public/react_embeddables/search/search_react_embeddable.tsx +++ b/examples/embeddable_examples/public/react_embeddables/search/search_react_embeddable.tsx @@ -15,7 +15,7 @@ import { useBatchedPublishingSubjects, } from '@kbn/presentation-publishing'; import React, { useEffect } from 'react'; -import { BehaviorSubject, switchMap } from 'rxjs'; +import { BehaviorSubject, switchMap, tap } from 'rxjs'; import { SEARCH_EMBEDDABLE_ID } from './constants'; import { getCount } from './get_count'; import { Api, Services, State } from './types'; @@ -55,8 +55,14 @@ export const getSearchEmbeddableFactory = (services: Services) => { const error$ = new BehaviorSubject(undefined); const count$ = new BehaviorSubject(0); + let prevRequestAbortController: AbortController | undefined; const fetchSubscription = fetch$(api) .pipe( + tap(() => { + if (prevRequestAbortController) { + prevRequestAbortController.abort(); + } + }), switchMap(async (fetchContext) => { error$.next(undefined); if (!defaultDataView) { @@ -65,6 +71,8 @@ export const getSearchEmbeddableFactory = (services: Services) => { try { dataLoading$.next(true); + const abortController = new AbortController(); + prevRequestAbortController = abortController; const count = await getCount( defaultDataView, services.data, @@ -79,11 +87,13 @@ export const getSearchEmbeddableFactory = (services: Services) => { to: new Date(fetchContext.timeslice[1]).toISOString(), mode: 'absolute' as 'absolute', } - : fetchContext.timeRange + : fetchContext.timeRange, + abortController.signal, + fetchContext.searchSessionId ); return { count }; } catch (error) { - return { error }; + return error.name === 'AbortError' ? undefined : { error }; } }) )