Mojang / ore-ui

💎 Building blocks to construct game UIs using web tech.
https://react-facet.mojang.com/
MIT License
404 stars 19 forks source link

Fix useFacetCallback mismatch subscriptions #115

Closed pirelenito closed 1 year ago

pirelenito commented 1 year ago

After the fix of #114, it introduced a new bug that would cause the callback to never being able to be called since useFacetCallback would internally might no longer have a facet with an active subscription.

The new bug was caused by using a shared noop instance across all usages of useFacetCallback, and it occurs because our createFacet implementation uses a given listener instance as a key to keep track of all its listeners. See: https://github.com/Mojang/ore-ui/blob/main/packages/%40react-facet/core/src/facet/createFacet.ts#L17

The fix makes sure that we don't reuse the same instance, with a accompanying unit test.

In the future we might want to review our decision of using a Set as it can potentially introduce issues such as this one. There has been some work on this front, but it comes with other challenges as well (potentially on performance and supporting unsubscribing as events are firing).