Skip to content

Commit c4414d9

Browse files
authored
fix: Fix route change timing fetch counter decrement (#145)
* fix: πŸ› fix issue that decrement fetch counter was called always Basically the finally call of fetch was not only being called on finally. * test: πŸ’ add test for fetchCounter update
1 parent 59947fc commit c4414d9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

β€Žsrc/sessions/VirtualPageLoadTimer.tsβ€Ž

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,12 @@ export class VirtualPageLoadTimer extends MonkeyPatched {
162162
input: RequestInfo,
163163
init?: RequestInit
164164
): Promise<Response> => {
165-
const self = this;
166165
return original
167166
.apply(thisArg, argsArray)
168167
.catch((error) => {
169168
throw error;
170169
})
171-
.finally(self.decrementFetchCounter());
170+
.finally(this.decrementFetchCounter);
172171
};
173172

174173
/**
@@ -190,12 +189,12 @@ export class VirtualPageLoadTimer extends MonkeyPatched {
190189
};
191190
};
192191

193-
private decrementFetchCounter() {
192+
private decrementFetchCounter = () => {
194193
if (!this.isPageLoaded) {
195194
this.latestEndTime = Date.now();
196195
}
197196
this.fetchCounter -= 1;
198-
}
197+
};
199198

200199
/**
201200
* Checks whether the virtual page is still being loaded.

β€Žsrc/sessions/__tests__/VirtualPageLoadTimer.test.tsβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ describe('VirtualPageLoadTimer tests', () => {
207207
expect(virtualPageLoadTimer['ongoingRequests'].size).toEqual(0);
208208
});
209209

210+
test('when fetch is fetch counter should be updated to 1 until finished', async () => {
211+
// Init
212+
const virtualPageLoadTimer = new VirtualPageLoadTimer(
213+
pageManager,
214+
config,
215+
record
216+
);
217+
218+
// Mocking Date.now to return 100 to simulate time passed
219+
Date.now = jest.fn(() => 100);
220+
virtualPageLoadTimer.startTiming();
221+
222+
// When fetch initially is sent, fetchCounter should be incremented to 1
223+
const fetching = fetch('https://aws.amazon.com');
224+
expect(virtualPageLoadTimer['fetchCounter']).toEqual(1);
225+
226+
await fetching;
227+
228+
// Upon completion, fetchCounter should be decremented to 0
229+
expect(virtualPageLoadTimer['fetchCounter']).toEqual(0);
230+
});
231+
210232
test('when fetch is detected during route change then latestEndTime is updated', async () => {
211233
// Init
212234
const virtualPageLoadTimer = new VirtualPageLoadTimer(

0 commit comments

Comments
Β (0)