Skip to content

Commit

Permalink
feat:Refetch DAG on tab change if not yet availabel (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsanteri committed Oct 29, 2021
1 parent 834c87f commit 0fb9165
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/hooks/useResource/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface Resource<T> {
getResult: () => DataModel<T> | null;
target: string;
status: AsyncStatus;
retry: () => void;
}

//
Expand Down Expand Up @@ -151,6 +152,8 @@ export default function useResource<T, U>({
const [status, statusDispatch] = useReducer(StatusReducer, { id: 0, status: 'NotAsked' });
const q = new URLSearchParams(queryParams).toString();
const target = apiHttp(`${url}${q ? '?' + q : ''}`);
const [retryCounter, setRetryCounter] = useState(0);

// Call batch update
useInterval(() => {
if (useBatching && onUpdate && updateBatcher[target] && (updateBatcher[target] as Array<U>).length > 0) {
Expand Down Expand Up @@ -299,9 +302,13 @@ export default function useResource<T, U>({
abortCtrl.abort();
}
};
}, [target, pause]); // eslint-disable-line
}, [target, pause, retryCounter]); // eslint-disable-line

function retry() {
setRetryCounter((val) => val + 1);
}

return { url, target, data, error, getResult: () => result, status: status.status };
return { url, target, data, error, getResult: () => result, status: status.status, retry };
}

function getWebsocketUrl<T>(url: string, data: T | null, wsUrl?: string | ((result: T) => string)): string {
Expand Down
6 changes: 6 additions & 0 deletions src/pages/Run/RunPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ const RunPage: React.FC<RunPageProps> = ({ run, params }) => {
subscribeToEvents: false,
initialData: null,
});
// Refetch dag on tab change if dag fetching failed
useEffect(() => {
if ((dagResult.status === 'Error' || dagResult.data === null) && tab === 'dag') {
dagResult.retry();
}
}, [tab]); //eslint-disable-line

return (
<>
Expand Down
1 change: 1 addition & 0 deletions src/utils/testhelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function createResource<T>(data: T, props: Partial<Resource<T>>): Resourc
getResult: () => null,
target: '',
status: 'Ok',
retry: () => null,
...props,
};
}
Expand Down

0 comments on commit 0fb9165

Please sign in to comment.