Skip to content
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

Merged
merged 6 commits into from
Mar 3, 2023

Conversation

pirelenito
Copy link
Member

@pirelenito pirelenito commented Mar 2, 2023

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 a useFacetMap 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.

@@ -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())
Copy link
Contributor

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.

Copy link
Member Author

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.

@Shenato
Copy link
Contributor

Shenato commented Mar 2, 2023

This looks good to me! great find!

@pirelenito pirelenito merged commit 65dd216 into main Mar 3, 2023
@pirelenito pirelenito deleted the make-sure-callbacks-get-current-value branch March 3, 2023 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants