Skip to content

Commit

Permalink
Clean up type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Heenawter committed Jan 25, 2023
1 parent ac8ac72 commit f0e2018
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,17 @@ export class OptionsListEmbeddable extends Embeddable<OptionsListEmbeddableInput
},
this.abortController.signal
);
if (!this.optionsListService.optionsListResponseWasSuccessful(response)) {
const error = (response as OptionsListFailureResponse).error;
if (error === 'aborted') {
if (this.optionsListService.optionsListResponseWasFailure(response)) {
if (response.error === 'aborted') {
// This prevents an aborted request (which can happen, for example, when a user types a search string too quickly)
// from prematurely setting loading to `false` and updating the suggestions to show "No results"
return;
}
dispatch(setLoading(false));
this.onFatalError(error);
this.onFatalError(response.error);
return;
}

const { suggestions, invalidSelections, totalCardinality } =
response as OptionsListSuccessResponse;
const { suggestions, invalidSelections, totalCardinality } = response;
if (
(!selectedOptions && !existsSelected) ||
isEmpty(invalidSelections) ||
Expand Down Expand Up @@ -427,9 +424,12 @@ export class OptionsListEmbeddable extends Embeddable<OptionsListEmbeddableInput
public onFatalError = (e: Error) => {
const {
dispatch,
actions: { setPopoverOpen },
actions: { setPopoverOpen, setLoading },
} = this.reduxEmbeddableTools;
dispatch(setPopoverOpen(false));
batch(() => {
dispatch(setLoading(false));
dispatch(setPopoverOpen(false));
});
super.onFatalError(e);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class OptionsListService implements ControlsOptionsListService {
}
};

public optionsListResponseWasSuccessful = (
public optionsListResponseWasFailure = (
response: OptionsListResponse
): response is OptionsListSuccessResponse => {
return Boolean((response as OptionsListSuccessResponse).suggestions);
): response is OptionsListFailureResponse => {
return (response as OptionsListFailureResponse).error !== undefined;
};

public runOptionsListRequest = async (request: OptionsListRequest, abortSignal: AbortSignal) => {
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/controls/public/services/options_list/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
* Side Public License, v 1.
*/

import { OptionsListRequest, OptionsListResponse } from '../../../common/options_list/types';
import {
OptionsListFailureResponse,
OptionsListRequest,
OptionsListResponse,
} from '../../../common/options_list/types';

export interface ControlsOptionsListService {
runOptionsListRequest: (
request: OptionsListRequest,
abortSignal: AbortSignal
) => Promise<OptionsListResponse>;
clearOptionsListCache: () => void;
optionsListResponseWasFailure: (
response: OptionsListResponse
) => response is OptionsListFailureResponse;
getAllowExpensiveQueries: () => Promise<boolean>;
optionsListResponseWasSuccessful: (response: OptionsListResponse) => boolean;
}

0 comments on commit f0e2018

Please sign in to comment.