Skip to content

Commit

Permalink
More PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Feb 15, 2021
1 parent 89f3df8 commit 1ef7e5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/plugins/es_ui_shared/public/request/send_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface SendRequestConfig {
method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
query?: HttpFetchQuery;
body?: any;
/**
* If set, flags this as a "system request" to indicate that this is not a user-initiated request. For more information, see
* HttpFetchOptions#asSystemRequest.
*/
asSystemRequest?: boolean;
}

Expand Down
14 changes: 12 additions & 2 deletions src/plugins/es_ui_shared/public/request/use_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export const useRequest = <D = any, E = Error>(
// "old" error/data or loading state when a new request is in-flight.
setIsLoading(true);

// Any requests that are sent in the background (without user interaction) should be flagged as "system requests". This should not be
// confused with any terminology in Elasticsearch. This is a Kibana-specific construct that allows the server to differentiate between
// user-initiated and requests "system"-initiated requests, for purposes like security features.
const requestPayload = { ...requestBody, asSystemRequest };
const response = await sendRequest<D, E>(httpClient, requestPayload);
const { data: serializedResponseData, error: responseError } = response;
Expand Down Expand Up @@ -111,7 +114,10 @@ export const useRequest = <D = any, E = Error>(
clearPollInterval();

if (pollIntervalMs) {
pollIntervalIdRef.current = setTimeout(() => resendRequest(true), pollIntervalMs);
pollIntervalIdRef.current = setTimeout(
() => resendRequest(true), // This is happening on an interval in the background, so we flag it as a "system request".
pollIntervalMs
);
}
}, [pollIntervalMs, resendRequest, clearPollInterval]);

Expand Down Expand Up @@ -141,11 +147,15 @@ export const useRequest = <D = any, E = Error>(
};
}, [clearPollInterval]);

const resendRequestForConsumer = useCallback(() => {
return resendRequest();
}, [resendRequest]);

return {
isInitialRequest: isInitialRequestRef.current,
isLoading,
error,
data,
resendRequest, // Gives the user the ability to manually request data
resendRequest: resendRequestForConsumer, // Gives the user the ability to manually request data
};
};

0 comments on commit 1ef7e5a

Please sign in to comment.