-
Notifications
You must be signed in to change notification settings - Fork 18
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 useFacetCallback having outdated values #114
Conversation
…b.com/Mojang/ore-ui into make-sure-callbacks-get-current-value
@@ -66,14 +62,16 @@ export function useFacetCallback<M, Y extends Facet<unknown>[], T extends [...Y] | |||
// eslint-disable-next-line react-hooks/exhaustive-deps | |||
return useCallback( | |||
(...args: K) => { | |||
const values = facetsRef.current | |||
const values = facets.map((facet) => facet.get()) |
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.
Should this be args.map()?
I'm not sure how to fix errors the the linter is throwing here.
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.
We should do it in the same way as in the useLayoutEffect
above. Just pushed a new commit.
This looks good to me! great find! |
The unit test demonstrates how the issue can happen, but basically if we have as the result of a subscription that a callback from a
useFacetCallback
is called, it could have outdated values.This would happen simply because the order of the subscriptions could have the internal subscription inside the
useFacetCallback
be notified after everything else.An implication of this change, is that the call to
get
will re-execute any transformations done by auseFacetMap
but I don't expect this to be of a concern.The
useFacetRef
will potentially have the same issue, but we can investigate and address in a follow-up PR.