Skip to content

Commit

Permalink
Core data: Expand type signature of useQuerySelect (#39656)
Browse files Browse the repository at this point in the history
Part of #39211

In this patch we're expanding the type signature of `useQuerySelect`
and fixing a minor type-related issue that was formerly there.

We want to do great things with the `core-data` type system but all of these
little nuisances get in the way when we're trying to study those things in
isolation. This is a preparatory change to eliminate some of the noise in the
existing types.

As a type-only change this will have no impact on the built code.
Please audit the types and the use of the wrapper `EnrichedSelectors`.
  • Loading branch information
dmsnell authored Mar 31, 2022
1 parent 004dcc6 commit aa3193a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/core-data/src/hooks/use-query-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const META_SELECTORS = [
'getCachedResolvers',
];

interface QuerySelectResponse {
interface QuerySelectResponse< Data > {
/** the requested selector return value */
data: Object;
data: Data;

/** is the record still being resolved? Via the `getIsResolving` meta-selector */
isResolving: boolean;
Expand Down Expand Up @@ -78,9 +78,14 @@ export default function __experimentalUseQuerySelect( mapQuerySelect, deps ) {
}, deps );
}

type QuerySelector = ( ...args ) => QuerySelectResponse;
interface EnrichedSelectors {
[ key: string ]: QuerySelector;
< Selectors extends Record< string, ( ...args: any[] ) => any > >(
selectors: Selectors
): {
[ Selector in keyof Selectors ]: (
...args: Parameters< Selectors[ Selector ] >
) => QuerySelectResponse< ReturnType< Selectors[ Selector ] > >;
};
}

/**
Expand All @@ -90,14 +95,14 @@ interface EnrichedSelectors {
* @param {Object} selectors Selectors to enrich
* @return {EnrichedSelectors} Enriched selectors
*/
const enrichSelectors = memoize( ( selectors ) => {
const enrichSelectors = memoize( ( ( selectors ) => {
const resolvers = {};
for ( const selectorName in selectors ) {
if ( META_SELECTORS.includes( selectorName ) ) {
continue;
}
Object.defineProperty( resolvers, selectorName, {
get: () => ( ...args ) => {
get: () => ( ...args: unknown[] ) => {
const { getIsResolving, hasFinishedResolution } = selectors;
const isResolving = !! getIsResolving( selectorName, args );
const hasResolved =
Expand Down Expand Up @@ -128,4 +133,4 @@ const enrichSelectors = memoize( ( selectors ) => {
} );
}
return resolvers;
} );
} ) as EnrichedSelectors );

0 comments on commit aa3193a

Please sign in to comment.