Skip to content

Commit

Permalink
react control group: implement reload (elastic#190366)
Browse files Browse the repository at this point in the history
PR adds reload implementation for react control group.
  • Loading branch information
nreese authored Aug 13, 2024
1 parent 74d8858 commit 571fe04
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export const ReactControlExample = ({
const saveNotification$ = useMemo(() => {
return new Subject<void>();
}, []);
const reload$ = useMemo(() => {
return new Subject<void>();
}, []);
const [dataLoading, timeRange, viewMode] = useBatchedPublishingSubjects(
dataLoading$,
timeRange$,
Expand Down Expand Up @@ -138,6 +141,7 @@ export const ReactControlExample = ({
},
lastUsedDataViewId: new BehaviorSubject<string>(WEB_LOGS_DATA_VIEW_ID),
saveNotification$,
reload$,
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down Expand Up @@ -381,6 +385,9 @@ export const ReactControlExample = ({
to: end,
});
}}
onRefresh={() => {
reload$.next();
}}
/>
<EuiSpacer size="m" />
<ReactEmbeddableRenderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PublishesDataViews,
useBatchedPublishingSubjects,
} from '@kbn/presentation-publishing';
import { apiPublishesReload } from '@kbn/presentation-publishing/interfaces/fetch/publishes_reload';
import { ControlStyle, ParentIgnoreSettings } from '../..';
import {
ControlGroupChainingSystem,
Expand Down Expand Up @@ -205,6 +206,7 @@ export const getControlGroupEmbeddableFactory = (services: {
saveNotification$: apiHasSaveNotification(parentApi)
? parentApi.saveNotification$
: undefined,
reload$: apiPublishesReload(parentApi) ? parentApi.reload$ : undefined,
});

/** Subscribe to all children's output data views, combine them, and output them */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@kbn/presentation-publishing';
import { PublishesDataViews } from '@kbn/presentation-publishing/interfaces/publishes_data_views';

import { PublishesReload } from '@kbn/presentation-publishing/interfaces/fetch/publishes_reload';
import { ParentIgnoreSettings } from '../..';
import { ControlInputTransform } from '../../../common';
import { ControlGroupChainingSystem } from '../../../common/control_group/types';
Expand Down Expand Up @@ -61,7 +62,7 @@ export type ControlGroupApi = PresentationContainer &
Pick<PublishesUnsavedChanges, 'unsavedChanges'> &
PublishesControlGroupDisplaySettings &
PublishesTimeslice &
Partial<HasParentApi<PublishesUnifiedSearch> & HasSaveNotification> & {
Partial<HasParentApi<PublishesUnifiedSearch> & HasSaveNotification & PublishesReload> & {
asyncResetUnsavedChanges: () => Promise<void>;
autoApplySelections$: PublishingSubject<boolean>;
controlFetch$: (controlUuid: string) => Observable<ControlFetchContext>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import {
combineLatest,
debounceTime,
Observable,
of,
startWith,
switchMap,
tap,
withLatestFrom,
} from 'rxjs';

import { PublishingSubject } from '@kbn/presentation-publishing';
import { apiPublishesReload } from '@kbn/presentation-publishing/interfaces/fetch/publishes_reload';
import { OptionsListSuccessResponse } from '../../../../../common/options_list/types';
import { isValidSearch } from '../../../../../common/options_list/is_valid_search';
import { OptionsListSelection } from '../../../../../common/options_list/options_list_selections';
Expand Down Expand Up @@ -57,6 +60,12 @@ export function fetchAndValidate$({
stateManager.searchTechnique,
// cannot use requestSize directly, because we need to be able to reset the size to the default without refetching
api.loadMoreSubject.pipe(debounceTime(100)), // debounce load more so "loading" state briefly shows
apiPublishesReload(api.parentApi)
? api.parentApi.reload$.pipe(
tap(() => requestCache.clearCache()),
startWith(undefined)
)
: of(undefined),
]).pipe(
tap(() => {
// abort any in progress requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ export class OptionsListFetchCache {
return result;
}
}

public clearCache = () => {
this.cache.reset();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const getRangesliderControlFactory = (
}
loadingMinMax$.next(isLoading);
},
controlGroupApi,
}).subscribe(
({
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@ import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { AggregateQuery, Filter, Query, TimeRange } from '@kbn/es-query';
import { PublishesDataViews, PublishingSubject } from '@kbn/presentation-publishing';
import { combineLatest, lastValueFrom, Observable, switchMap, tap } from 'rxjs';
import { combineLatest, lastValueFrom, Observable, of, startWith, switchMap, tap } from 'rxjs';
import { apiPublishesReload } from '@kbn/presentation-publishing/interfaces/fetch/publishes_reload';
import { ControlFetchContext } from '../../../control_group/control_fetch';
import { ControlGroupApi } from '../../../control_group/types';

export function minMax$({
controlFetch$,
controlGroupApi,
data,
dataViews$,
fieldName$,
setIsLoading,
}: {
controlFetch$: Observable<ControlFetchContext>;
controlGroupApi: ControlGroupApi;
data: DataPublicPluginStart;
dataViews$: PublishesDataViews['dataViews'];
fieldName$: PublishingSubject<string>;
setIsLoading: (isLoading: boolean) => void;
}) {
let prevRequestAbortController: AbortController | undefined;
return combineLatest([controlFetch$, dataViews$, fieldName$]).pipe(
return combineLatest([
controlFetch$,
dataViews$,
fieldName$,
apiPublishesReload(controlGroupApi)
? controlGroupApi.reload$.pipe(startWith(undefined))
: of(undefined),
]).pipe(
tap(() => {
if (prevRequestAbortController) {
prevRequestAbortController.abort();
Expand Down

0 comments on commit 571fe04

Please sign in to comment.