Skip to content

Commit

Permalink
TS is not bad
Browse files Browse the repository at this point in the history
  • Loading branch information
fuziontech committed Jan 9, 2025
1 parent 29ec8b7 commit a624f06
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions frontend/src/utils/usePollingEffect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { useEffect, useRef } from 'react'

export function usePollingEffect(
asyncCallback: any,
dependencies = [],
dependencies: any[] = [],
{
interval = 3000, // 3 seconds,
onCleanUp = () => {},
onCleanUp = () => { },
} = {}
) {
const timeoutIdRef = useRef<number | null>(null)
useEffect(() => {
let _stopped = false
;(async function pollingCallback() {
try {
await asyncCallback()
} finally {
// Set timeout after it finished, unless stopped
timeoutIdRef.current = !_stopped && window.setTimeout(pollingCallback, interval)
}
})()
; (async function pollingCallback() {
try {
await asyncCallback()
} finally {
// Set timeout after it finished, unless stopped
timeoutIdRef.current = !_stopped && window.setTimeout(pollingCallback, interval)
}
})()
// Clean up if dependencies change
return () => {
_stopped = true // prevent racing conditions
Expand Down

0 comments on commit a624f06

Please sign in to comment.