Skip to content

Commit b3414b7

Browse files
committed
Apply Seb's comments re comments.
1 parent fc2f91e commit b3414b7

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/plugins/es_ui_shared/public/request/use_request.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ export const useRequest = <D = any, E = Error>(
8585
// data, to avoid doubled-up requests.
8686
clearPollInterval();
8787

88-
// Pull off request ID and increment request count.
89-
const requestId = requestCountRef.current++;
88+
const requestId = ++requestCountRef.current;
9089

9190
// We don't clear error or data, so it's up to the consumer to decide whether to display the
9291
// "old" error/data or loading state when a new request is in-flight.
@@ -95,7 +94,7 @@ export const useRequest = <D = any, E = Error>(
9594
const response = await sendStatelessRequest<D, E>(httpClient, requestBody);
9695
const { data: serializedResponseData, error: responseError } = response;
9796

98-
const isOutdatedRequest = requestId !== requestCountRef.current - 1;
97+
const isOutdatedRequest = requestId !== requestCountRef.current;
9998
const isUnmounted = isMounted.current === false;
10099

101100
// Ignore outdated or irrelevant data.
@@ -121,7 +120,6 @@ export const useRequest = <D = any, E = Error>(
121120
// If there's a scheduled poll request, this new one will supersede it.
122121
clearPollInterval();
123122

124-
// Schedule next poll request.
125123
if (pollIntervalMs) {
126124
pollIntervalIdRef.current = setTimeout(sendRequest, pollIntervalMs);
127125
}
@@ -135,7 +133,8 @@ export const useRequest = <D = any, E = Error>(
135133
// Schedule the next poll request when the previous one completes.
136134
useEffect(() => {
137135
// When a request completes, attempt to schedule the next one. Note that we aren't re-scheduling
138-
// a request whenever sendRequest's dependencies change.
136+
// a request whenever sendRequest's dependencies change. isLoading isn't set to false until the
137+
// initial request has completed, so we won't schedule a request on mount.
139138
if (!isLoading) {
140139
scheduleRequest();
141140
}

0 commit comments

Comments
 (0)