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

Core data: Expand type signature of useQuerySelect #39656

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );