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

Cached maps (useFacetMemo) can return values before any subscription #139

Merged
merged 4 commits into from
Jul 25, 2024

Conversation

pirelenito
Copy link
Member

The choice of useFacetMap or useFacetMemo should be based on performance metrics and what is best for each use-case. So, they should share as much as possible in terms of behaviour and API.

Currently, there is one small difference between then into how they handle attempts to read their values via a get before any subscription has started.

With useFacetMap, because it has no internal caching, it always reads the source data when attempting to read its value through a get, so it would be possible to get the transformed mapped value even if no subscriptions were started.

However, with useFacetMemo because it has a cached value, it would always just return that value instead. Meaning you could get an innitial NO_VALUE with useFacetMemo when you would get the actual value with useFacetMap.

This PR fixes this inconsistency, so both hooks behave the same when trying to read their values ahead of any subscription.

Copy link
Contributor

@Shenato Shenato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like it makes a lot of sense, we will have to double check and test to see if any users relied on the old behavior for any reason before fully merging this. but otherwise looks good 👍

@pirelenito
Copy link
Member Author

That looks like it makes a lot of sense, we will have to double check and test to see if any users relied on the old behavior for any reason before fully merging this. but otherwise looks good 👍

This change came from us identifying a bug actually where it would only happen when using useFacetMemo.

@pirelenito pirelenito merged commit ce93421 into main Jul 25, 2024
12 checks passed
@pirelenito pirelenito deleted the cached-map-before-subscription branch July 25, 2024 09:17
marlonicus added a commit that referenced this pull request Aug 12, 2024
…139)

* Cached maps can return values before any subscription

* Update packages/@react-facet/core/src/mapFacets/mapFacets.spec.ts

Co-authored-by: Marlon <[email protected]>

* Update packages/@react-facet/core/src/mapFacets/mapFacets.spec.ts

---------

Co-authored-by: Marlon <[email protected]>
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