Skip to content

Commit

Permalink
Clean up abort controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Heenawter committed Jul 19, 2024
1 parent 78c2f60 commit 9e24c2d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/plugins/discover/public/embeddable/initialize_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { BehaviorSubject, combineLatest, lastValueFrom, switchMap } from 'rxjs';
import { BehaviorSubject, combineLatest, lastValueFrom, switchMap, tap } from 'rxjs';

import { KibanaExecutionContext } from '@kbn/core/types';
import {
Expand Down Expand Up @@ -91,22 +91,24 @@ export function initializeFetch({
discoverServices: DiscoverServices;
}) {
const requestAdapter = new RequestAdapter();
let abortController = new AbortController();
let abortController: AbortController | undefined;

const fetchSubscription = combineLatest([fetch$(api), api.savedSearch$, api.dataViews])
.pipe(
tap(() => {
// abort any in-progress requests
if (abortController) {
abortController.abort();
abortController = undefined;
}
}),
switchMap(async ([fetchContext, savedSearch, dataViews]) => {
const dataView = dataViews?.length ? dataViews[0] : undefined;
api.blockingError.next(undefined);
if (!dataView || !savedSearch.searchSource) {
return;
}

// Abort any in-progress requests
const currentAbortController = new AbortController();
abortController.abort();
abortController = currentAbortController;

const useNewFieldsApi = !discoverServices.uiSettings.get(SEARCH_FIELDS_FROM_SOURCE, false);
updateSearchSource(
discoverServices,
Expand All @@ -126,6 +128,7 @@ export function initializeFetch({

try {
api.dataLoading.next(true);

// Log request to inspector
requestAdapter.reset();
await discoverServices.profilesManager.resolveDataSourceProfile({
Expand All @@ -138,6 +141,10 @@ export function initializeFetch({
query: searchSourceQuery,
});

// Get new abort controller
const currentAbortController = new AbortController();
abortController = currentAbortController;

const esqlMode = isEsqlMode(savedSearch);
if (
esqlMode &&
Expand Down

0 comments on commit 9e24c2d

Please sign in to comment.