Skip to content

Commit

Permalink
fix: add types to useSuspenseSelect (WordPress#60733)
Browse files Browse the repository at this point in the history
* fix: add types to useSuspenseSelect

This commit use existing types to more completely type the `useSuspenseSelect` hook.
Using a generic `MapSelect` type for the argument and it's return type will provide
exact typings of the provided `select` function when using a typed data store object.

* add @throws JSDoc for suspense promise

* fix: remove incorrect @typedef
  • Loading branch information
johnhooks authored Apr 15, 2024
1 parent 158d3b9 commit c53c910
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,16 +859,16 @@ _Returns_

### useSuspenseSelect

A variant of the `useSelect` hook that has the same API, but will throw a suspense Promise if any of the called selectors is in an unresolved state.
A variant of the `useSelect` hook that has the same API, but is a compatible Suspense-enabled data source.

_Parameters_

- _mapSelect_ `Function`: Function called on every state change. The returned value is exposed to the component using this hook. The function receives the `registry.suspendSelect` method as the first argument and the `registry` as the second one.
- _mapSelect_ `T`: Function called on every state change. The returned value is exposed to the component using this hook. The function receives the `registry.suspendSelect` method as the first argument and the `registry` as the second one.
- _deps_ `Array`: A dependency array used to memoize the `mapSelect` so that the same `mapSelect` is invoked on every state change unless the dependencies change.

_Returns_

- `Object`: Data object returned by the `mapSelect` function.
- `ReturnType<T>`: Data object returned by the `mapSelect` function.

### withDispatch

Expand Down
26 changes: 15 additions & 11 deletions packages/data/src/components/use-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,23 @@ export default function useSelect( mapSelect, deps ) {
}

/**
* A variant of the `useSelect` hook that has the same API, but will throw a
* suspense Promise if any of the called selectors is in an unresolved state.
* A variant of the `useSelect` hook that has the same API, but is a compatible
* Suspense-enabled data source.
*
* @param {Function} mapSelect Function called on every state change. The
* returned value is exposed to the component
* using this hook. The function receives the
* `registry.suspendSelect` method as the first
* argument and the `registry` as the second one.
* @param {Array} deps A dependency array used to memoize the `mapSelect`
* so that the same `mapSelect` is invoked on every
* state change unless the dependencies change.
* @template {MapSelect} T
* @param {T} mapSelect Function called on every state change. The
* returned value is exposed to the component
* using this hook. The function receives the
* `registry.suspendSelect` method as the first
* argument and the `registry` as the second one.
* @param {Array} deps A dependency array used to memoize the `mapSelect`
* so that the same `mapSelect` is invoked on every
* state change unless the dependencies change.
*
* @return {Object} Data object returned by the `mapSelect` function.
* @throws {Promise} A suspense Promise that is thrown if any of the called
* selectors is in an unresolved state.
*
* @return {ReturnType<T>} Data object returned by the `mapSelect` function.
*/
export function useSuspenseSelect( mapSelect, deps ) {
return useMappingSelect( true, mapSelect, deps );
Expand Down

0 comments on commit c53c910

Please sign in to comment.