From 4a2488c32fb1eea250ed311f836ee55dbebd48ae Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Tue, 5 Nov 2024 17:45:28 +0530 Subject: [PATCH 1/3] TypeScript: Convert factory utils in data package to TS --- packages/core-data/src/private-selectors.ts | 7 +++- ...{create-selector.js => create-selector.ts} | 6 ++-- packages/data/src/{factory.js => factory.ts} | 35 +++++++++++++------ 3 files changed, 33 insertions(+), 15 deletions(-) rename packages/data/src/{create-selector.js => create-selector.ts} (55%) rename packages/data/src/{factory.js => factory.ts} (76%) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index b2f6fa7def985..ac058a34c2505 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -56,7 +56,12 @@ export const getBlockPatternsForPostType = createRegistrySelector( */ export const getEntityRecordsPermissions = createRegistrySelector( ( select ) => createSelector( - ( state: State, kind: string, name: string, ids: string[] ) => { + ( + state: State, + kind: string, + name: string, + ids: string | string[] + ) => { const normalizedIds = Array.isArray( ids ) ? ids : [ ids ]; return normalizedIds.map( ( id ) => ( { delete: select( STORE_NAME ).canUser( 'delete', { diff --git a/packages/data/src/create-selector.js b/packages/data/src/create-selector.ts similarity index 55% rename from packages/data/src/create-selector.js rename to packages/data/src/create-selector.ts index 069932e8007e2..6d05dff1ca036 100644 --- a/packages/data/src/create-selector.js +++ b/packages/data/src/create-selector.ts @@ -4,8 +4,8 @@ * * @see The documentation for the `rememo` package from which the `createSelector` function is reexported. * - * @param {Function} selector Selector function that calculates a value from state and parameters. - * @param {Function} getDependants Function that returns an array of "dependant" objects. - * @return {Function} A memoized version of `selector` that caches the calculated return values. + * @param selector Selector function that calculates a value from state and parameters. + * @param getDependants Function that returns an array of "dependant" objects. + * @return A memoized version of `selector` that caches the calculated return values. */ export { default as createSelector } from 'rememo'; diff --git a/packages/data/src/factory.js b/packages/data/src/factory.ts similarity index 76% rename from packages/data/src/factory.js rename to packages/data/src/factory.ts index be4ef8cf673c5..14726c26c4389 100644 --- a/packages/data/src/factory.js +++ b/packages/data/src/factory.ts @@ -1,3 +1,8 @@ +/** + * Internal dependencies + */ +import type { select as globalSelect } from './select'; + /** * Creates a selector function that takes additional curried argument with the * registry `select` function. While a regular selector has signature @@ -33,17 +38,25 @@ * registry as argument. The registry binding happens automatically when registering the selector * with a store. * - * @param {Function} registrySelector Function receiving a registry `select` - * function and returning a state selector. + * @param registrySelector Function receiving a registry `select` + * function and returning a state selector. * - * @return {Function} Registry selector that can be registered with a store. + * @return Registry selector that can be registered with a store. */ -export function createRegistrySelector( registrySelector ) { +export function createRegistrySelector< + Selector extends ( ...args: any[] ) => any, +>( registrySelector: ( select: typeof globalSelect ) => Selector ): Selector { + type WrappedSelector = { + ( ...args: Parameters< Selector > ): ReturnType< Selector >; + isRegistrySelector: boolean; + registry?: any; + }; + const selectorsByRegistry = new WeakMap(); // Create a selector function that is bound to the registry referenced by `selector.registry` // and that has the same API as a regular selector. Binding it in such a way makes it // possible to call the selector directly from another selector. - const wrappedSelector = ( ...args ) => { + const wrappedSelector: WrappedSelector = ( ...args ) => { let selector = selectorsByRegistry.get( wrappedSelector.registry ); // We want to make sure the cache persists even when new registry // instances are created. For example patterns create their own editors @@ -60,12 +73,10 @@ export function createRegistrySelector( registrySelector ) { * Flag indicating that the selector is a registry selector that needs the correct registry * reference to be assigned to `selector.registry` to make it work correctly. * be mapped as a registry selector. - * - * @type {boolean} */ wrappedSelector.isRegistrySelector = true; - return wrappedSelector; + return wrappedSelector as unknown as Selector; } /** @@ -84,11 +95,13 @@ export function createRegistrySelector( registrySelector ) { * When registering a control created with `createRegistryControl` with a store, the store * knows which calling convention to use when executing the control. * - * @param {Function} registryControl Function receiving a registry object and returning a control. + * @param registryControl Function receiving a registry object and returning a control. * - * @return {Function} Registry control that can be registered with a store. + * @return Registry control that can be registered with a store. */ -export function createRegistryControl( registryControl ) { +export function createRegistryControl< T extends ( ...args: any ) => any >( + registryControl: T & { isRegistryControl?: boolean } +) { registryControl.isRegistryControl = true; return registryControl; From 2514dbba278fddc1f12f3c18a887faffa5dd51b8 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 6 Nov 2024 14:13:53 +0530 Subject: [PATCH 2/3] Fix docgen build error --- packages/data/src/create-selector.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/data/src/create-selector.ts b/packages/data/src/create-selector.ts index 6d05dff1ca036..bfb7a1d283733 100644 --- a/packages/data/src/create-selector.ts +++ b/packages/data/src/create-selector.ts @@ -3,9 +3,5 @@ * and the selector parameters, and recomputes the values only when any of them changes. * * @see The documentation for the `rememo` package from which the `createSelector` function is reexported. - * - * @param selector Selector function that calculates a value from state and parameters. - * @param getDependants Function that returns an array of "dependant" objects. - * @return A memoized version of `selector` that caches the calculated return values. */ export { default as createSelector } from 'rememo'; From 7a6106b7ba6f53fe00e1b07aeeb6960259386c83 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 6 Nov 2024 14:40:39 +0530 Subject: [PATCH 3/3] Update docs --- packages/data/README.md | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/data/README.md b/packages/data/README.md index 25dd75820fb5d..07562c6bc89f9 100644 --- a/packages/data/README.md +++ b/packages/data/README.md @@ -423,11 +423,11 @@ When registering a control created with `createRegistryControl` with a store, th _Parameters_ -- _registryControl_ `Function`: Function receiving a registry object and returning a control. +- _registryControl_ `T & { isRegistryControl?: boolean; }`: Function receiving a registry object and returning a control. _Returns_ -- `Function`: Registry control that can be registered with a store. +- Registry control that can be registered with a store. ### createRegistrySelector @@ -476,11 +476,11 @@ with a store. _Parameters_ -- _registrySelector_ `Function`: Function receiving a registry `select` function and returning a state selector. +- _registrySelector_ `( select: ) => Selector`: Function receiving a registry `select` function and returning a state selector. _Returns_ -- `Function`: Registry selector that can be registered with a store. +- `Selector`: Registry selector that can be registered with a store. ### createSelector @@ -490,15 +490,6 @@ _Related_ - The documentation for the `rememo` package from which the `createSelector` function is reexported. -_Parameters_ - -- _selector_ `Function`: Selector function that calculates a value from state and parameters. -- _getDependants_ `Function`: Function that returns an array of "dependant" objects. - -_Returns_ - -- `Function`: A memoized version of `selector` that caches the calculated return values. - ### dispatch Given a store descriptor, returns an object of the store's action creators. Calling an action creator will cause it to be dispatched, updating the state value accordingly.