Skip to content

Commit 130e495

Browse files
committed
Reduces view refresh freq for fetch time
1 parent 0fe5034 commit 130e495

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/git/utils/fetch.utils.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,47 @@ const millisecondsPerMinute = 60 * 1000;
22
const millisecondsPerHour = 60 * 60 * 1000;
33
export const millisecondsPerDay = 24 * 60 * 60 * 1000;
44

5+
/**
6+
* Calculates the update interval for fetch status based on how long ago the last fetch occurred.
7+
* Uses stepped intervals to prevent thrashing from frequent recalculations.
8+
*
9+
* @param lastFetched - Timestamp (in milliseconds) of the last fetch
10+
* @returns Update interval in milliseconds
11+
*
12+
* Interval schedule:
13+
* - < 15 minutes ago: refresh every 5 minutes
14+
* - < 30 minutes ago: refresh every 10 minutes
15+
* - < 1 hour ago: refresh every 20 minutes
16+
* - < 3 hours ago: refresh every 1 hour
17+
* - < 6 hours ago: refresh every 2 hours
18+
* - < 12 hours ago: refresh every 4 hours
19+
* - < 2 days ago: refresh every 8 hours
20+
* - ≥ 2 days ago: refresh every 1 day
21+
*/
522
export function getLastFetchedUpdateInterval(lastFetched: number): number {
623
const timeDiff = Date.now() - lastFetched;
7-
return timeDiff < millisecondsPerDay
8-
? (timeDiff < millisecondsPerHour ? millisecondsPerMinute : millisecondsPerHour) / 2
9-
: 0;
24+
25+
// Stepped intervals to prevent thrashing
26+
if (timeDiff < 15 * millisecondsPerMinute) {
27+
return 5 * millisecondsPerMinute;
28+
}
29+
if (timeDiff < 30 * millisecondsPerMinute) {
30+
return 10 * millisecondsPerMinute;
31+
}
32+
if (timeDiff < millisecondsPerHour) {
33+
return 20 * millisecondsPerMinute;
34+
}
35+
if (timeDiff < 3 * millisecondsPerHour) {
36+
return millisecondsPerHour;
37+
}
38+
if (timeDiff < 6 * millisecondsPerHour) {
39+
return 2 * millisecondsPerHour;
40+
}
41+
if (timeDiff < 12 * millisecondsPerHour) {
42+
return 4 * millisecondsPerHour;
43+
}
44+
if (timeDiff < 2 * millisecondsPerDay) {
45+
return 8 * millisecondsPerHour;
46+
}
47+
return millisecondsPerDay;
1048
}

0 commit comments

Comments
 (0)