Skip to content

Commit

Permalink
Update searchSessionId on setDateRange
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Dec 9, 2024
1 parent 183d0d2 commit 219ccb3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export function useDatePicker({
(newDateRange: TimeRange) => {
setUrlState({ dateRange: newDateRange });
setParsedDateRange(parseDateRange(newDateRange));
updateSearchSessionId();
},
[setUrlState]
[setUrlState, updateSearchSessionId]
);

const onRefresh = useCallback(
Expand All @@ -62,12 +63,10 @@ export function useDatePicker({
if (autoRefreshEnabled) {
autoRefreshTick$.next(null);
} else {
updateSearchSessionId();
setDateRange(newDateRange);
}

setDateRange(newDateRange);
},
[autoRefreshEnabled, autoRefreshTick$, setDateRange, updateSearchSessionId]
[autoRefreshEnabled, autoRefreshTick$, setDateRange]
);

const setAutoRefresh = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { useCallback, useEffect, useMemo } from 'react';
import { SearchSessionState, waitUntilNextSessionCompletes$ } from '@kbn/data-plugin/public';
import { useKibanaContextForPlugin } from '../../../hooks/use_kibana';
import { useDatePickerContext } from './use_date_picker';
import { useSearchSessionContext } from '../../../hooks/use_search_session';

export type RequestState = 'running' | 'done' | 'error';
const WAIT_MS = 1000;
Expand All @@ -37,16 +36,11 @@ export const useLoadingState = () => {
const {
data: { search },
} = services;
const { updateSearchSessionId } = useSearchSessionContext();

const isAutoRefreshRequestPending$ = useMemo(() => new BehaviorSubject<boolean>(false), []);
const requestsCount$ = useMemo(() => new BehaviorSubject(0), []);
const requestState$ = useMemo(() => new BehaviorSubject<RequestState | null>(null), []);

useEffect(() => {
updateSearchSessionId();
}, [updateSearchSessionId]);

const waitUntilRequestsCompletes$ = useCallback(
() =>
requestsCount$.pipe(
Expand Down Expand Up @@ -132,27 +126,17 @@ export const useLoadingState = () => {

autoRefreshTick$.pipe(
skipUntil(isAutoRefreshEnabled$()),
withLatestFrom(search.session.state$, isAutoRefreshRequestPending$),
switchMap(([_, state, isAutoRefreshRequestPending]) => {
withLatestFrom(search.session.state$),
switchMap(([_, state]) => {
// if the current state$ value is not Completed
if (state === SearchSessionState.Loading) {
// Wait until queries using data.search complete before processing the next tick
// This will only be called when Lens is used in the Asset Details page
return waitUntilNextSessionCompletes$(search.session).pipe(
tap(() => {
updateSearchSessionId();
})
);
return waitUntilNextSessionCompletes$(search.session);
}

// Else immediately emit true if session state is already completed
return of(null).pipe(
tap(() => {
if (!isAutoRefreshRequestPending) {
updateSearchSessionId();
}
})
);
return of(null);
})
)
).subscribe();
Expand All @@ -169,7 +153,7 @@ export const useLoadingState = () => {
requestState$,
requestsCount$,
search.session,
updateSearchSessionId,
// updateSearchSessionId,
waitUntilRequestsCompletes$,
]);

Expand Down

0 comments on commit 219ccb3

Please sign in to comment.