-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(key-management-service): add polling after KMS order
ref: MANAGER-14904 Signed-off-by: David Arsène <[email protected]>
- Loading branch information
Showing
3 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/manager/apps/key-management-service/src/data/hooks/useAutoRefetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
type AutoRefetchProps = { | ||
queryKey: string[]; | ||
enabled: boolean; | ||
interval?: number; | ||
timeout?: number; | ||
onFinish?: () => void; | ||
}; | ||
|
||
export const useAutoRefetch = ({ | ||
queryKey, | ||
enabled = false, | ||
interval = 30_000, | ||
timeout = 60_000 * 5, | ||
onFinish, | ||
}: AutoRefetchProps) => { | ||
const [isEnabled, setIsEnabled] = useState<boolean>(enabled); | ||
const queryClient = useQueryClient(); | ||
|
||
useEffect(() => { | ||
if (!isEnabled) { | ||
return undefined; | ||
} | ||
const refetchTimeout = setTimeout(() => { | ||
setIsEnabled(false); | ||
onFinish?.(); | ||
}, timeout); | ||
const refetchInterval = setInterval( | ||
() => queryClient.invalidateQueries({ queryKey }), | ||
interval, | ||
); | ||
|
||
return () => { | ||
clearInterval(refetchInterval); | ||
clearTimeout(refetchTimeout); | ||
}; | ||
}, [isEnabled, interval, timeout, queryKey, queryClient]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters