Skip to content

Commit 153d829

Browse files
committed
fix: prevent ghost autofetch
1 parent f7f38c4 commit 153d829

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/utils/autofetcher.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export class AutoFetcher {
33
this.timeout = AutoFetcher.DEFAULT_TIMEOUT;
44
this.active = true;
55
this.timer = undefined;
6+
this.launchCounter = 0;
67
}
78

89
wait(ms: number) {
@@ -16,13 +17,21 @@ export class AutoFetcher {
1617
return;
1718
}
1819

20+
const currentLaunch = this.launchCounter;
21+
1922
await this.wait(this.timeout);
2023

2124
if (this.active) {
2225
const startTs = Date.now();
2326
await request();
2427
const finishTs = Date.now();
2528

29+
if (currentLaunch !== this.launchCounter) {
30+
// autofetcher was restarted while request was in progress
31+
// stop further fetches, we are in deprecated thread
32+
return;
33+
}
34+
2635
const responseTime = finishTs - startTs;
2736
const nextTimeout =
2837
responseTime > AutoFetcher.MIN_TIMEOUT ? responseTime : AutoFetcher.MIN_TIMEOUT;
@@ -40,6 +49,7 @@ export class AutoFetcher {
4049
this.active = false;
4150
}
4251
start() {
52+
this.launchCounter++;
4353
this.active = true;
4454
}
4555

@@ -48,4 +58,5 @@ export class AutoFetcher {
4858
timeout: number;
4959
active: boolean;
5060
timer: undefined | ReturnType<typeof setTimeout>;
61+
launchCounter: number;
5162
}

0 commit comments

Comments
 (0)