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

[feature] Add useDenormalized() hook selector #134

Merged
merged 1 commit into from Sep 16, 2019
Merged

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Sep 11, 2019

Motivation

The goal is to make useDenormalized() the way useCache() selects data - removing the automatic drilling into results to find the entity inside.

schema:

{
  data: getEntitySchema(this)
}

Before

const article = useResource(Article.detailShape(), { id });

After

const { data: article } = useResource(Article.detailShape(), { id });

Before changing useCache(), we will add this new selector hook and compose it with the 'drill in' behavior to achieve backwards compatible results. While mostly this should be an unnoticed change, we do export the useDenormalized() for those who wish to use it.

Solution

The 'drill in' is now basically all useSchemaSelect() does, and it calls useDenormalized() first to get the initial stuff. Splitting this up now to provide an elegant migration path before we deprecate the old method. 3.0 will use the new method in the normal names, while still providing the old selector in deprecated hooks (so people can just rename for migration). 3.1 will then remove the legacy hooks.

/**
 * Selects the denormalized form from `state` cache.
 *
 * If `result` is not found, will attempt to generate it naturally
 * using params and schema. This increases cache hit rate for many
 * detail shapes.
 */
function useDenormalized<
  Params extends Readonly<object>,
  S extends Schema
>(
  fetchShape: Pick<ReadShape<S, Params, any>, 'schema' | 'getFetchKey'>,
  params: Params | null,
  state: State<any>,
): Denormalized<typeof schema> | null;

Note

Will add useCacheNew() and useResourceNew() in followup PR

@ntucker ntucker changed the title [feature] Add useDenormalized() hook selector [WIP][feature] Add useDenormalized() hook selector Sep 11, 2019
@ntucker ntucker force-pushed the select-rewrite branch 3 times, most recently from 091f8b3 to 95bd98f Compare September 14, 2019 23:04
@ntucker ntucker changed the title [WIP][feature] Add useDenormalized() hook selector [feature] Add useDenormalized() hook selector Sep 14, 2019
@ntucker ntucker changed the title [feature] Add useDenormalized() hook selector [feature] Add useDenormalized(), useCacheNew() and useResourceNew() Sep 14, 2019
@ntucker ntucker changed the title [feature] Add useDenormalized(), useCacheNew() and useResourceNew() [feature] Add useDenormalized() hook selector Sep 15, 2019
@ntucker ntucker merged commit d121b11 into master Sep 16, 2019
@ntucker ntucker deleted the select-rewrite branch September 16, 2019 20:31
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.

None yet

2 participants