Skip to content

Commit

Permalink
Add AbortController to fetchFields and change translation id in annot…
Browse files Browse the repository at this point in the history
…ations_editor
  • Loading branch information
DianaDerevyankina committed Nov 27, 2020
1 parent 129ad49 commit fafc0d4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class AnnotationsEditor extends Component {
helpText={
defaultIndexPattern &&
!model.index_pattern &&
i18n.translate('visTypeTimeseries.indexPattern.searchByDefaultIndex', {
i18n.translate('visTypeTimeseries.annotationsEditor.searchByDefaultIndex', {
defaultMessage: 'Default index pattern is used. To query all indexes use *',
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ import { Storage } from '../../../../../plugins/kibana_utils/public';
const VIS_STATE_DEBOUNCE_DELAY = 200;
const APP_NAME = 'VisEditor';

const debouncedFetchFields = debounce(
(extractedIndexPatterns) => fetchFields(extractedIndexPatterns),
VIS_STATE_DEBOUNCE_DELAY,
{ leading: true }
);

export class VisEditor extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -82,6 +76,19 @@ export class VisEditor extends Component {
});
}, VIS_STATE_DEBOUNCE_DELAY);

fetchFields = debounce(
(extractedIndexPatterns) => {
if (this.abortControllerFetchFields) {
this.abortControllerFetchFields.abort();
}
this.abortControllerFetchFields = new AbortController();

return fetchFields(extractedIndexPatterns, this.abortControllerFetchFields.signal);
},
VIS_STATE_DEBOUNCE_DELAY,
{ leading: true }
);

handleChange = (partialModel) => {
if (isEmpty(partialModel)) {
return;
Expand All @@ -100,7 +107,7 @@ export class VisEditor extends Component {

const extractedIndexPatterns = extractIndexPatterns(nextModel);
if (!isEqual(this.state.extractedIndexPatterns, extractedIndexPatterns)) {
debouncedFetchFields(extractedIndexPatterns).then((visFields) =>
this.fetchFields(extractedIndexPatterns).then((visFields) =>
this.setState({
visFields,
extractedIndexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,38 @@ import { extractIndexPatterns } from '../../../common/extract_index_patterns';
import { getCoreStart } from '../../services';
import { ROUTES } from '../../../common/constants';

export async function fetchFields(indexPatterns = []) {
export async function fetchFields(indexPatterns = [], signal) {
const patterns = Array.isArray(indexPatterns) ? indexPatterns : [indexPatterns];
try {
const indexFields = await Promise.all(
patterns.map((pattern) => {
return getCoreStart().http.get(ROUTES.FIELDS, {
patterns.map((pattern) =>
getCoreStart().http.get(ROUTES.FIELDS, {
query: {
index: pattern,
},
});
})
signal,
})
)
);
const fields = patterns.reduce((cumulatedFields, currentPattern, index) => {
return {

return patterns.reduce(
(cumulatedFields, currentPattern, index) => ({
...cumulatedFields,
[currentPattern]: indexFields[index],
};
}, {});
return fields;
} catch (error) {
getCoreStart().notifications.toasts.addDanger({
title: i18n.translate('visTypeTimeseries.fetchFields.loadIndexPatternFieldsErrorMessage', {
defaultMessage: 'Unable to load index_pattern fields',
}),
text: error.message,
});
{}
);
} catch (error) {
if (error.name !== 'AbortError') {
getCoreStart().notifications.toasts.addDanger({
title: i18n.translate('visTypeTimeseries.fetchFields.loadIndexPatternFieldsErrorMessage', {
defaultMessage: 'Unable to load index_pattern fields',
}),
text: error.message,
});
}
}
return [];
}

export async function fetchIndexPatternFields({ params, fields = {} }) {
Expand Down

0 comments on commit fafc0d4

Please sign in to comment.