Skip to content

Commit

Permalink
fix: Avoid page flickering on pod completion
Browse files Browse the repository at this point in the history
Fixes #14. The subscription no longer resets when the interval changes,
but only when the route or its arguments change.
  • Loading branch information
meyfa committed Jul 7, 2024
1 parent 2a6bd6f commit ced8a7a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions frontend/src/api/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ export function useApiSubscription <T extends {}, Args extends unknown[]> (
const { url, method, body } = route.request(...args)

const { fetch, reset } = fetcher

// Start with empty data and error initially, and every time the arguments change.
useEffect(() => {
// This effect hook is called once initially, and then every time the arguments change.
// As such, we want to start with empty data and error.
reset()
}, [reset, route, url, method, body])

// Subscribe to the route.
useEffect(() => {
// Prevent subscriptions to non-nilpotent routes.
if (method != null && method !== 'GET') {
return
Expand All @@ -48,7 +52,7 @@ export function useApiSubscription <T extends {}, Args extends unknown[]> (
abortController?.abort()
clearInterval(interval)
}
}, [fetch, reset, options.interval, route, url, method, body])
}, [fetch, options.interval, route, url, method, body])

const loading = fetcher.data == null && fetcher.error == null

Expand Down

0 comments on commit ced8a7a

Please sign in to comment.