Skip to content

Commit

Permalink
feat(topology): entity resource should auto refresh if enabled (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuan Vo authored Apr 17, 2023
1 parent 86438b6 commit 772575f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/app/Topology/Shared/Entity/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
concatMap,
defaultIfEmpty,
forkJoin,
interval,
map,
merge,
Observable,
Expand Down Expand Up @@ -387,7 +388,7 @@ export const useResources = <R = ResourceTypes,>(
targetNode: TargetNode,
resourceType: TargetOwnedResourceType | TargetRelatedResourceType
): { resources: R[]; error?: Error; loading?: boolean } => {
const { api, notificationChannel } = React.useContext(ServiceContext);
const { api, notificationChannel, settings } = React.useContext(ServiceContext);
const addSubscription = useSubscriptions();

const [resources, setResources] = React.useState<ResourceTypes[]>([]);
Expand All @@ -402,7 +403,7 @@ export const useResources = <R = ResourceTypes,>(
targetSubject
.pipe(
switchMap((tn) => {
return getTargetOwnedResources(resourceType, tn, api).pipe(
const resourceObs = getTargetOwnedResources(resourceType, tn, api).pipe(
map((rs: ResourceTypes[]) => ({
resources: rs,
error: undefined,
Expand All @@ -414,6 +415,13 @@ export const useResources = <R = ResourceTypes,>(
})
)
);
if (!settings.autoRefreshEnabled()) {
return resourceObs;
}
return merge(
resourceObs,
interval(settings.autoRefreshPeriod() * settings.autoRefreshUnits()).pipe(concatMap(() => resourceObs))
);
})
)
.subscribe(({ resources, error }) => {
Expand All @@ -422,7 +430,7 @@ export const useResources = <R = ResourceTypes,>(
setResources(resources);
})
);
}, [addSubscription, setLoading, setError, setResources, api, targetSubject, resourceType]);
}, [addSubscription, setLoading, setError, setResources, api, settings, targetSubject, resourceType]);

React.useEffect(() => {
const patchEventConfig = [
Expand Down

0 comments on commit 772575f

Please sign in to comment.