-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix es_ui_shared eslint violations for useRequest hook #59626
Fix es_ui_shared eslint violations for useRequest hook #59626
Conversation
Pinging @elastic/es-ui (Team:Elasticsearch UI) |
2fd8696
to
29ba8b5
Compare
5af8709
to
9914f70
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM, also tested a checkout, thanks for the update!
9914f70
to
1a58bc1
Compare
body, | ||
pollIntervalMs, | ||
initialData, | ||
deserializer = (data: any): any => data, | ||
}: UseRequestConfig | ||
): UseRequestResponse => { | ||
): UseRequestResponse<D, E> => { | ||
const isMounted = useRef(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to handle the case when the owner component unmounts before a request has resolved.
@sebelga Could you please test Index Management, Watcher, and Snapshot Restore and see if you spot any bugs regarding requests? |
@elastic/ingest-management Could someone from your team please test the Ingest Manager and make sure I haven't introduced any buggy behavior on your end? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ran a smoke test of Ingest Manager locally and things are working as expected 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Tested locally watcher, IM, and S & R and did not spot any regression.
if (pollInterval.current) { | ||
pollIntervalId.current = setTimeout(_sendRequest, pollInterval.current); | ||
if (pollIntervalMsRef.current && isMounted.current) { | ||
pollIntervalIdRef.current = setTimeout( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: isn't it the same as
pollIntervalIdRef.current = setTimeout(
sendRequestRef.current!,
pollIntervalMsRef.current
);
): UseRequestResponse<D, E> => { | ||
const isMounted = useRef(true); | ||
const sendRequestRef = useRef<() => Promise<SendRequestResponse<D, E>>>(); | ||
const scheduleRequestRef = useRef<() => void>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why you created a ref here? I would have used useCallback
for this purpose. Was it necessary to add the ref?
I see that sendRequestRef
is also a ref. I think useCallback
is better for this purpose. Maybe I'm missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think switching to useCallback
is an interesting idea and could yield optimization benefits by returning a memoized reference to sendRequest
. It would look something like this, though I'm not sure about the implications of mutating a ref you're dependent upon (any risk of infinite loop?):
const scheduleRequest = useCallback(
() => {
// Clear current interval
if (pollIntervalIdRef.current) {
clearTimeout(pollIntervalIdRef.current);
}
// Set new interval
if (pollIntervalMsRef.current && isMounted.current) {
pollIntervalIdRef.current = setTimeout(
() => sendRequestRef.current!(),
pollIntervalMsRef.current
);
}
},
[pollIntervalIdRef.current, pollIntervalMsRef.current, isMounted.current, pollIntervalIdRef.current, sendRequestRef.current],
);
However, the original NP-ready code uses refs and I find them easier to reason about than the useCallback
example above, so I think I'll leave optimization to another day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it would imply a bigger refactor (e.g. why do we have a ref for pollInterval
?), but I agree we can leave that to another day. 👍
- Add clearer cleanup logic for unmounted components. - Align logic and comments in np_ready_request.ts and original request.ts.
2a52625
to
b0c309a
Compare
…o false during cleanups unrelated to unmounting.
b0c309a
to
616d4a6
Compare
I just noticed a subtle change in the way return {
data: null,
error: e.response ? e.response : e,
}; To this: return {
data: null,
error: e.response && e.response.data ? e.response.data : e.body,
}; To find and fix any bugs means we'll need to look through the plugins which depend upon the old behavior (Snapshot Restore and Ingest Manager). @jen-huang Can you or someone from the Ingest team please audit Ingest Manager and make sure errors returned by |
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
This branch is too stale to continue on. Closing in favor of #72947 |
Partially addresses #49554
Closes #49572 (originally fixed as part of the NP migration)
Closes #49562 (originally fixed as part of the NP migration)
This PR:
request.ts
helper and replaces it with the NP-ready versionAdds clean-up logic for when the component using this helper is unmounted(This was reverted)To test: