-
Notifications
You must be signed in to change notification settings - Fork 17
fix: write intelligent timing to query #2853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
379b94f
fix: write intelligent timing to query
astandrik 9959a5b
fix: review fixes
astandrik fa0d027
fix: small refactor
astandrik 0a7202b
Merge branch 'main' into astandrik.2845
astandrik 5397d4e
Apply suggestion from @greptile-apps[bot]
astandrik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,63 @@ | ||
| import React from 'react'; | ||
|
|
||
| import {SECOND_IN_MS} from '../../utils/constants'; | ||
|
|
||
| const FAST_REFRESH_MS = 100; | ||
| const FAST_REFRESH_JITTER_MS = 20; | ||
| const TEN_SECONDS_IN_MS = 10 * SECOND_IN_MS; | ||
|
|
||
| interface UseElapsedDurationParams { | ||
| startTime?: number; | ||
| endTime?: number; | ||
| loading?: boolean; | ||
| } | ||
|
|
||
| export function useElapsedDuration({ | ||
| startTime, | ||
| endTime, | ||
| loading, | ||
| }: UseElapsedDurationParams): number { | ||
| const [durationMs, setDurationMs] = React.useState<number>( | ||
| startTime ? (endTime || Date.now()) - startTime : 0, | ||
| ); | ||
|
|
||
| const setDuration = React.useCallback(() => { | ||
| if (startTime) { | ||
| const actualEndTime = endTime || Date.now(); | ||
| setDurationMs(actualEndTime - startTime); | ||
| } | ||
| }, [endTime, startTime]); | ||
|
|
||
| const timerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined); | ||
| const cancelledRef = React.useRef<boolean>(false); | ||
|
|
||
| const tick = React.useCallback(() => { | ||
| if (cancelledRef.current) { | ||
| return; | ||
| } | ||
| setDuration(); | ||
|
|
||
| if (!loading || !startTime || cancelledRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| const actualEndTime = endTime || Date.now(); | ||
| const elapsedMs = actualEndTime - startTime; | ||
| const jitter = Math.floor((Math.random() * 2 - 1) * FAST_REFRESH_JITTER_MS); | ||
astandrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const nextDelay = elapsedMs < TEN_SECONDS_IN_MS ? FAST_REFRESH_MS + jitter : SECOND_IN_MS; | ||
| timerRef.current = setTimeout(tick, nextDelay); | ||
| }, [setDuration, loading, startTime, endTime]); | ||
|
|
||
| React.useEffect(() => { | ||
| cancelledRef.current = false; | ||
| tick(); | ||
| return () => { | ||
| cancelledRef.current = true; | ||
| if (timerRef.current) { | ||
| clearTimeout(timerRef.current); | ||
| } | ||
| }; | ||
| }, [tick]); | ||
astandrik marked this conversation as resolved.
Show resolved
Hide resolved
astandrik marked this conversation as resolved.
Show resolved
Hide resolved
astandrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return durationMs; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.