Skip to content

Commit

Permalink
TypeScript: Convert factory utils in data package to TS (WordPress#67667
Browse files Browse the repository at this point in the history
)

* TypeScript: Convert factory utils in data package to TS

* Fix docgen build error

* Update docs

* Revert and fix TS error

* Extract and improve signature
  • Loading branch information
manzoorwanijk authored and yogeshbhutkar committed Dec 18, 2024
1 parent 5610588 commit c49eb9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function getEntityRecordPermissions(
name: string,
id: string
) {
return getEntityRecordsPermissions( state, kind, name, id )[ 0 ];
return getEntityRecordsPermissions( state, kind, name, [ id ] )[ 0 ];
}

/**
Expand Down
17 changes: 4 additions & 13 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,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
Expand Down Expand Up @@ -471,11 +471,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.
- `RegistrySelector< Selector >`: Registry selector that can be registered with a store.
### createSelector
Expand All @@ -485,15 +485,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {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.
*/
export { default as createSelector } from 'rememo';
35 changes: 25 additions & 10 deletions packages/data/src/factory.js → packages/data/src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* Internal dependencies
*/
import type { select as globalSelect } from './select';

type RegistrySelector< Selector extends ( ...args: any[] ) => any > = {
( ...args: Parameters< Selector > ): ReturnType< Selector >;
isRegistrySelector?: boolean;
registry?: any;
};

/**
* Creates a selector function that takes additional curried argument with the
* registry `select` function. While a regular selector has signature
Expand Down Expand Up @@ -33,17 +44,21 @@
* 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
): RegistrySelector< Selector > {
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: RegistrySelector< Selector > = ( ...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
Expand All @@ -60,8 +75,6 @@ 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;

Expand All @@ -84,11 +97,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;
Expand Down

0 comments on commit c49eb9e

Please sign in to comment.