Skip to content

Commit

Permalink
bring back updateSessionId usage on useLoadingState
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Dec 10, 2024
1 parent 4b614d8 commit 8852912
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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 @@ -36,11 +37,16 @@ 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 @@ -126,17 +132,27 @@ export const useLoadingState = () => {

autoRefreshTick$.pipe(
skipUntil(isAutoRefreshEnabled$()),
withLatestFrom(search.session.state$),
switchMap(([_, state]) => {
withLatestFrom(search.session.state$, isAutoRefreshRequestPending$),
switchMap(([_, state, isAutoRefreshRequestPending]) => {
// 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);
return waitUntilNextSessionCompletes$(search.session).pipe(
tap(() => {
updateSearchSessionId();
})
);
}

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

Expand Down

0 comments on commit 8852912

Please sign in to comment.