Skip to content

Commit

Permalink
[Discover] Experiment with debounced UnifiedHistogram props
Browse files Browse the repository at this point in the history
  • Loading branch information
jughosta committed Nov 13, 2024
1 parent 4ba25f2 commit a2be0c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Ref } from 'react';
import { debounce } from 'lodash';
import type {
UnifiedHistogramContainerProps,
UnifiedHistogramApi,
} from '@kbn/unified-histogram-plugin/public';

type Props = Omit<UnifiedHistogramContainerProps, 'container'> & {
ref: Ref<UnifiedHistogramApi>;
};
let debouncedProps: Props | undefined;

const updateDebouncedProps = debounce(
(props: Props) => {
// console.log('updated props', props.timeRange, props.filters?.length);
debouncedProps = props;
},
100,
{ leading: false, trailing: true }
);

export const getDebouncedHistogramProps = (props: Props): Props => {
if (!debouncedProps) {
debouncedProps = props;
} else {
// console.log('next props', props.timeRange, props.filters?.length);
updateDebouncedProps(props);
}

return debouncedProps;
};

export const clearDebouncedHistogramProps = () => {
debouncedProps = undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import type { DiscoverAppState } from '../../state_management/discover_app_state
import { DataDocumentsMsg } from '../../state_management/discover_data_state_container';
import { useSavedSearch } from '../../state_management/discover_state_provider';
import { useIsEsqlMode } from '../../hooks/use_is_esql_mode';
import {
getDebouncedHistogramProps,
clearDebouncedHistogramProps,
} from './get_debounced_histogram_props';

const EMPTY_ESQL_COLUMNS: DatatableColumn[] = [];
const EMPTY_FILTERS: Filter[] = [];
Expand Down Expand Up @@ -381,7 +385,13 @@ export const useDiscoverHistogram = ({
[stateContainer]
);

return {
useEffect(() => {
return () => {
clearDebouncedHistogramProps();
};
}, []);

return getDebouncedHistogramProps({
ref,
getCreationOptions,
services,
Expand All @@ -402,7 +412,7 @@ export const useDiscoverHistogram = ({
? savedSearchState?.visContext
: undefined,
onVisContextChanged: isEsqlMode ? onVisContextChanged : undefined,
};
});
};

// Use pairwise to diff the previous and current state (starting with undefined to ensure
Expand Down

0 comments on commit a2be0c5

Please sign in to comment.