Skip to content

Commit

Permalink
Make some data fetching hooks state updates atomic (#132069) (#132143)
Browse files Browse the repository at this point in the history
(cherry picked from commit 56dedf3)

Co-authored-by: Kevin Qualters <[email protected]>
  • Loading branch information
kibanamachine and kqualters-elastic authored May 12, 2022
1 parent 9c72255 commit a1743f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { isEmpty, isEqual, isUndefined, keyBy, pick } from 'lodash/fp';
import memoizeOne from 'memoize-one';
import { useCallback, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import type { DataViewBase } from '@kbn/es-query';
import { Subscription } from 'rxjs';

Expand All @@ -23,6 +24,7 @@ import {
import { isCompleteResponse, isErrorResponse } from '../../../../../../../src/plugins/data/common';
import * as i18n from './translations';
import { useAppToasts } from '../../hooks/use_app_toasts';
import { getDataViewStateFromIndexFields } from './use_data_view';

export type { BrowserField, BrowserFields, DocValueFields };

Expand Down Expand Up @@ -155,19 +157,27 @@ export const useFetchIndex = (
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
const stringifyIndices = response.indicesExist.sort().join();

previousIndexesName.current = response.indicesExist;
setLoading(false);
setState({
browserFields: getBrowserFields(stringifyIndices, response.indexFields),
docValueFields: getDocValueFields(stringifyIndices, response.indexFields),
indexes: response.indicesExist,
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
Promise.resolve().then(() => {
ReactDOM.unstable_batchedUpdates(() => {
const stringifyIndices = response.indicesExist.sort().join();

previousIndexesName.current = response.indicesExist;
const { browserFields, docValueFields } = getDataViewStateFromIndexFields(
stringifyIndices,
response.indexFields
);
setLoading(false);
setState({
browserFields,
docValueFields,
indexes: response.indicesExist,
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
});

searchSubscription$.current.unsubscribe();
});
});

searchSubscription$.current.unsubscribe();
} else if (isErrorResponse(response)) {
setLoading(false);
addWarning(i18n.ERROR_BEAT_FIELDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface DataViewInfo {
* HOT Code path where the fields can be 16087 in length or larger. This is
* VERY mutatious on purpose to improve the performance of the transform.
*/
const getDataViewStateFromIndexFields = memoizeOne(
export const getDataViewStateFromIndexFields = memoizeOne(
(_title: string, fields: IndexField[]): DataViewInfo => {
// Adds two dangerous casts to allow for mutations within this function
type DangerCastForMutation = Record<string, {}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { isEmpty } from 'lodash/fp';
import { useCallback, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import deepEqual from 'fast-deep-equal';
import { Subscription } from 'rxjs';

Expand Down Expand Up @@ -90,11 +91,15 @@ export const useTimelineEventsDetails = ({
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
setLoading(false);
setTimelineDetailsResponse(response.data || []);
setRawEventData(response.rawResponse.hits.hits[0]);
setEcsData(response.ecs || null);
searchSubscription$.current.unsubscribe();
Promise.resolve().then(() => {
ReactDOM.unstable_batchedUpdates(() => {
setLoading(false);
setTimelineDetailsResponse(response.data || []);
setRawEventData(response.rawResponse.hits.hits[0]);
setEcsData(response.ecs || null);
searchSubscription$.current.unsubscribe();
});
});
} else if (isErrorResponse(response)) {
setLoading(false);
addWarning(i18n.FAIL_TIMELINE_DETAILS);
Expand Down

0 comments on commit a1743f9

Please sign in to comment.