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

Calls to callbacks created via useFacetCallback can get outdated values #118

Open
xaviervia opened this issue Mar 14, 2023 · 2 comments
Open

Comments

@xaviervia
Copy link
Contributor

In a scenario where a function that is listening on a facet is calling another function created with useFacetCallback, the value of the same facet inside the second callback might be outdated.

For example:

const facet = createFacet({ initialValue: 'old value' })

function Comp() {
  // Since this is run on useLayoutEffect, it will
  // add the 
  const callback = useFacetCallback((value) => value, [], [facet])

  // Since this is run on render, it will add the
  // `.observe` subscription first
  useMemo(() => {
    facet.observe((value) => {
      console.log('here be new value: ', value)
      console.log('...and here is still old value: ', callback())
    })
  })
  
  return null
}

facet.set('new value')
@xaviervia
Copy link
Contributor Author

The fix should probably be that useFacetCallback reads the facet values instead of listening to changes in them. It's unclear why we went with the first one.

@xaviervia
Copy link
Contributor Author

Was actually fixed by https://github.com/Mojang/ore-ui/releases/tag/v0.4.4

This issue also exists in the useFacetRef BTW cc @pirelenito

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

No branches or pull requests

1 participant