@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
8 lines (7 loc) • 5.37 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/use-query-select.ts"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport memoize from './memoize';\nimport { Status } from './constants';\n\nexport const META_SELECTORS = [\n\t'getIsResolving',\n\t'hasStartedResolution',\n\t'hasFinishedResolution',\n\t'isResolving',\n\t'getCachedResolvers',\n];\n\ninterface QuerySelectResponse< Data > {\n\t/** the requested selector return value */\n\tdata: Data;\n\n\t/** is the record still being resolved? Via the `isResolving` meta-selector */\n\tisResolving: boolean;\n\n\t/** was the resolution started? Via the `hasStartedResolution` meta-selector */\n\thasStarted: boolean;\n\n\t/** has the resolution finished? Via the `hasFinishedResolution` meta-selector. */\n\thasResolved: boolean;\n}\n\n/**\n * Like useSelect, but the selectors return objects containing\n * both the original data AND the resolution info.\n *\n * @since 6.1.0 Introduced in WordPress core.\n * @private\n *\n * @param {Function} mapQuerySelect see useSelect\n * @param {Array} deps see useSelect\n *\n * @example\n * ```js\n * import { useQuerySelect } from '@wordpress/data';\n * import { store as coreDataStore } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { data: page, isResolving } = useQuerySelect( ( query ) => {\n * return query( coreDataStore ).getEntityRecord( 'postType', 'page', id )\n * }, [ id ] );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return page.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 10 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using the `mapSelect` callback on `useQuerySelect`.\n *\n * If the id prop changes then any page in the state for that id is\n * retrieved. If the id prop doesn't change and other props are passed in\n * that do change, the title will not change because the dependency is just\n * the id.\n * @see useSelect\n *\n * @return {QuerySelectResponse} Queried data.\n */\nexport default function useQuerySelect( mapQuerySelect, deps ) {\n\treturn useSelect( ( select, registry ) => {\n\t\tconst resolve = ( store ) => enrichSelectors( select( store ) );\n\t\treturn mapQuerySelect( resolve, registry );\n\t}, deps );\n}\n\ninterface EnrichedSelectors {\n\t< Selectors extends Record< string, ( ...args: any[] ) => any > >(\n\t\tselectors: Selectors\n\t): {\n\t\t[ Selector in keyof Selectors ]: (\n\t\t\t...args: Parameters< Selectors[ Selector ] >\n\t\t) => QuerySelectResponse< ReturnType< Selectors[ Selector ] > >;\n\t};\n}\n\n/**\n * Transform simple selectors into ones that return an object with the\n * original return value AND the resolution info.\n *\n * @param {Object} selectors Selectors to enrich\n * @return {EnrichedSelectors} Enriched selectors\n */\nconst enrichSelectors = memoize( ( ( selectors ) => {\n\tconst resolvers = {};\n\tfor ( const selectorName in selectors ) {\n\t\tif ( META_SELECTORS.includes( selectorName ) ) {\n\t\t\tcontinue;\n\t\t}\n\t\tObject.defineProperty( resolvers, selectorName, {\n\t\t\tget:\n\t\t\t\t() =>\n\t\t\t\t( ...args: unknown[] ) => {\n\t\t\t\t\tconst data = selectors[ selectorName ]( ...args );\n\t\t\t\t\tconst resolutionStatus = selectors.getResolutionState(\n\t\t\t\t\t\tselectorName,\n\t\t\t\t\t\targs\n\t\t\t\t\t)?.status;\n\n\t\t\t\t\tlet status;\n\t\t\t\t\tswitch ( resolutionStatus ) {\n\t\t\t\t\t\tcase 'resolving':\n\t\t\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 'finished':\n\t\t\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 'error':\n\t\t\t\t\t\t\tstatus = Status.Error;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase undefined:\n\t\t\t\t\t\t\tstatus = Status.Idle;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata,\n\t\t\t\t\t\tstatus,\n\t\t\t\t\t\tisResolving: status === Status.Resolving,\n\t\t\t\t\t\thasStarted: status !== Status.Idle,\n\t\t\t\t\t\thasResolved:\n\t\t\t\t\t\t\tstatus === Status.Success ||\n\t\t\t\t\t\t\tstatus === Status.Error,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t} );\n\t}\n\treturn resolvers;\n} ) as EnrichedSelectors );\n"],
"mappings": ";AAGA,SAAS,iBAAiB;AAK1B,OAAO,aAAa;AACpB,SAAS,cAAc;AAEhB,IAAM,iBAAiB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AA2De,SAAR,eAAiC,gBAAgB,MAAO;AAC9D,SAAO,UAAW,CAAE,QAAQ,aAAc;AACzC,UAAM,UAAU,CAAE,UAAW,gBAAiB,OAAQ,KAAM,CAAE;AAC9D,WAAO,eAAgB,SAAS,QAAS;AAAA,EAC1C,GAAG,IAAK;AACT;AAmBA,IAAM,kBAAkB,SAAW,CAAE,cAAe;AACnD,QAAM,YAAY,CAAC;AACnB,aAAY,gBAAgB,WAAY;AACvC,QAAK,eAAe,SAAU,YAAa,GAAI;AAC9C;AAAA,IACD;AACA,WAAO,eAAgB,WAAW,cAAc;AAAA,MAC/C,KACC,MACA,IAAK,SAAqB;AACzB,cAAM,OAAO,UAAW,YAAa,EAAG,GAAG,IAAK;AAChD,cAAM,mBAAmB,UAAU;AAAA,UAClC;AAAA,UACA;AAAA,QACD,GAAG;AAEH,YAAI;AACJ,gBAAS,kBAAmB;AAAA,UAC3B,KAAK;AACJ,qBAAS,OAAO;AAChB;AAAA,UACD,KAAK;AACJ,qBAAS,OAAO;AAChB;AAAA,UACD,KAAK;AACJ,qBAAS,OAAO;AAChB;AAAA,UACD,KAAK;AACJ,qBAAS,OAAO;AAChB;AAAA,QACF;AAEA,eAAO;AAAA,UACN;AAAA,UACA;AAAA,UACA,aAAa,WAAW,OAAO;AAAA,UAC/B,YAAY,WAAW,OAAO;AAAA,UAC9B,aACC,WAAW,OAAO,WAClB,WAAW,OAAO;AAAA,QACpB;AAAA,MACD;AAAA,IACF,CAAE;AAAA,EACH;AACA,SAAO;AACR,EAAyB;",
"names": []
}