@wordpress/data 10.41.0 → 10.41.1-next.v.202603161435.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +20 -28
  2. package/build/components/async-mode-provider/context.cjs +1 -1
  3. package/build/components/async-mode-provider/context.cjs.map +3 -3
  4. package/build/components/async-mode-provider/index.cjs +1 -1
  5. package/build/components/async-mode-provider/index.cjs.map +1 -1
  6. package/build/components/async-mode-provider/use-async-mode.cjs +1 -1
  7. package/build/components/async-mode-provider/use-async-mode.cjs.map +3 -3
  8. package/build/components/registry-provider/context.cjs +1 -1
  9. package/build/components/registry-provider/context.cjs.map +1 -1
  10. package/build/components/registry-provider/index.cjs +1 -1
  11. package/build/components/registry-provider/index.cjs.map +1 -1
  12. package/build/components/registry-provider/use-registry.cjs +1 -1
  13. package/build/components/registry-provider/use-registry.cjs.map +3 -3
  14. package/build/components/use-dispatch/index.cjs +1 -1
  15. package/build/components/use-dispatch/index.cjs.map +1 -1
  16. package/build/components/use-dispatch/use-dispatch-with-map.cjs +1 -1
  17. package/build/components/use-dispatch/use-dispatch-with-map.cjs.map +3 -3
  18. package/build/components/use-dispatch/use-dispatch.cjs +1 -1
  19. package/build/components/use-dispatch/use-dispatch.cjs.map +3 -3
  20. package/build/components/use-select/index.cjs +2 -2
  21. package/build/components/use-select/index.cjs.map +3 -3
  22. package/build/components/with-dispatch/index.cjs +1 -1
  23. package/build/components/with-dispatch/index.cjs.map +3 -3
  24. package/build/components/with-registry/index.cjs +1 -1
  25. package/build/components/with-registry/index.cjs.map +3 -3
  26. package/build/components/with-select/index.cjs +1 -1
  27. package/build/components/with-select/index.cjs.map +3 -3
  28. package/build/controls.cjs +7 -6
  29. package/build/controls.cjs.map +3 -3
  30. package/build/default-registry.cjs +3 -2
  31. package/build/default-registry.cjs.map +3 -3
  32. package/build/dispatch.cjs +3 -1
  33. package/build/dispatch.cjs.map +2 -2
  34. package/build/factory.cjs.map +2 -2
  35. package/build/index.cjs +9 -8
  36. package/build/index.cjs.map +3 -3
  37. package/build/plugins/index.cjs +1 -1
  38. package/build/plugins/index.cjs.map +1 -1
  39. package/build/plugins/persistence/index.cjs +13 -8
  40. package/build/plugins/persistence/index.cjs.map +3 -3
  41. package/build/plugins/persistence/storage/default.cjs +1 -1
  42. package/build/plugins/persistence/storage/default.cjs.map +3 -3
  43. package/build/plugins/persistence/storage/object.cjs +1 -1
  44. package/build/plugins/persistence/storage/object.cjs.map +3 -3
  45. package/build/promise-middleware.cjs +2 -1
  46. package/build/promise-middleware.cjs.map +3 -3
  47. package/build/redux-store/index.cjs +32 -16
  48. package/build/redux-store/index.cjs.map +3 -3
  49. package/build/redux-store/metadata/selectors.cjs +1 -1
  50. package/build/redux-store/metadata/selectors.cjs.map +3 -3
  51. package/build/redux-store/metadata/types.cjs +19 -0
  52. package/build/redux-store/metadata/types.cjs.map +7 -0
  53. package/build/registry.cjs +11 -7
  54. package/build/registry.cjs.map +3 -3
  55. package/build/resolvers-cache-middleware.cjs +6 -3
  56. package/build/resolvers-cache-middleware.cjs.map +3 -3
  57. package/build/select.cjs +3 -1
  58. package/build/select.cjs.map +2 -2
  59. package/build/store/index.cjs +1 -1
  60. package/build/store/index.cjs.map +3 -3
  61. package/build/types.cjs.map +1 -1
  62. package/build-module/components/async-mode-provider/context.mjs +1 -1
  63. package/build-module/components/async-mode-provider/context.mjs.map +3 -3
  64. package/build-module/components/async-mode-provider/index.mjs +1 -1
  65. package/build-module/components/async-mode-provider/index.mjs.map +1 -1
  66. package/build-module/components/async-mode-provider/use-async-mode.mjs +1 -1
  67. package/build-module/components/async-mode-provider/use-async-mode.mjs.map +3 -3
  68. package/build-module/components/registry-provider/context.mjs +1 -1
  69. package/build-module/components/registry-provider/context.mjs.map +1 -1
  70. package/build-module/components/registry-provider/index.mjs +1 -1
  71. package/build-module/components/registry-provider/index.mjs.map +1 -1
  72. package/build-module/components/registry-provider/use-registry.mjs +1 -1
  73. package/build-module/components/registry-provider/use-registry.mjs.map +3 -3
  74. package/build-module/components/use-dispatch/index.mjs +1 -1
  75. package/build-module/components/use-dispatch/index.mjs.map +1 -1
  76. package/build-module/components/use-dispatch/use-dispatch-with-map.mjs +1 -1
  77. package/build-module/components/use-dispatch/use-dispatch-with-map.mjs.map +3 -3
  78. package/build-module/components/use-dispatch/use-dispatch.mjs +1 -1
  79. package/build-module/components/use-dispatch/use-dispatch.mjs.map +3 -3
  80. package/build-module/components/use-select/index.mjs +2 -2
  81. package/build-module/components/use-select/index.mjs.map +3 -3
  82. package/build-module/components/with-dispatch/index.mjs +1 -1
  83. package/build-module/components/with-dispatch/index.mjs.map +3 -3
  84. package/build-module/components/with-registry/index.mjs +1 -1
  85. package/build-module/components/with-registry/index.mjs.map +3 -3
  86. package/build-module/components/with-select/index.mjs +1 -1
  87. package/build-module/components/with-select/index.mjs.map +3 -3
  88. package/build-module/controls.mjs +7 -6
  89. package/build-module/controls.mjs.map +3 -3
  90. package/build-module/default-registry.mjs +3 -2
  91. package/build-module/default-registry.mjs.map +3 -3
  92. package/build-module/dispatch.mjs +3 -1
  93. package/build-module/dispatch.mjs.map +2 -2
  94. package/build-module/factory.mjs.map +2 -2
  95. package/build-module/index.mjs +4 -3
  96. package/build-module/index.mjs.map +2 -2
  97. package/build-module/plugins/index.mjs +1 -1
  98. package/build-module/plugins/index.mjs.map +1 -1
  99. package/build-module/plugins/persistence/index.mjs +13 -8
  100. package/build-module/plugins/persistence/index.mjs.map +3 -3
  101. package/build-module/plugins/persistence/storage/default.mjs +1 -1
  102. package/build-module/plugins/persistence/storage/default.mjs.map +3 -3
  103. package/build-module/plugins/persistence/storage/object.mjs +1 -1
  104. package/build-module/plugins/persistence/storage/object.mjs.map +3 -3
  105. package/build-module/promise-middleware.mjs +2 -1
  106. package/build-module/promise-middleware.mjs.map +3 -3
  107. package/build-module/redux-store/index.mjs +32 -16
  108. package/build-module/redux-store/index.mjs.map +3 -3
  109. package/build-module/redux-store/metadata/selectors.mjs +1 -1
  110. package/build-module/redux-store/metadata/selectors.mjs.map +3 -3
  111. package/build-module/redux-store/metadata/types.mjs +1 -0
  112. package/build-module/redux-store/metadata/types.mjs.map +7 -0
  113. package/build-module/registry.mjs +11 -7
  114. package/build-module/registry.mjs.map +3 -3
  115. package/build-module/resolvers-cache-middleware.mjs +6 -3
  116. package/build-module/resolvers-cache-middleware.mjs.map +3 -3
  117. package/build-module/select.mjs +3 -1
  118. package/build-module/select.mjs.map +2 -2
  119. package/build-module/store/index.mjs +1 -1
  120. package/build-module/store/index.mjs.map +3 -3
  121. package/build-types/components/async-mode-provider/context.d.ts +37 -3
  122. package/build-types/components/async-mode-provider/context.d.ts.map +1 -1
  123. package/build-types/components/async-mode-provider/index.d.ts +2 -2
  124. package/build-types/components/async-mode-provider/index.d.ts.map +1 -1
  125. package/build-types/components/async-mode-provider/use-async-mode.d.ts.map +1 -1
  126. package/build-types/components/registry-provider/context.d.ts +10 -3
  127. package/build-types/components/registry-provider/context.d.ts.map +1 -1
  128. package/build-types/components/registry-provider/index.d.ts +2 -2
  129. package/build-types/components/registry-provider/index.d.ts.map +1 -1
  130. package/build-types/components/registry-provider/use-registry.d.ts +3 -2
  131. package/build-types/components/registry-provider/use-registry.d.ts.map +1 -1
  132. package/build-types/components/use-dispatch/index.d.ts +2 -2
  133. package/build-types/components/use-dispatch/index.d.ts.map +1 -1
  134. package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts +11 -9
  135. package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts.map +1 -1
  136. package/build-types/components/use-dispatch/use-dispatch.d.ts +10 -19
  137. package/build-types/components/use-dispatch/use-dispatch.d.ts.map +1 -1
  138. package/build-types/components/use-select/index.d.ts +24 -28
  139. package/build-types/components/use-select/index.d.ts.map +1 -1
  140. package/build-types/components/with-dispatch/index.d.ts +10 -11
  141. package/build-types/components/with-dispatch/index.d.ts.map +1 -1
  142. package/build-types/components/with-registry/index.d.ts +2 -6
  143. package/build-types/components/with-registry/index.d.ts.map +1 -1
  144. package/build-types/components/with-select/index.d.ts +7 -8
  145. package/build-types/components/with-select/index.d.ts.map +1 -1
  146. package/build-types/controls.d.ts +57 -44
  147. package/build-types/controls.d.ts.map +1 -1
  148. package/build-types/default-registry.d.ts +2 -2
  149. package/build-types/default-registry.d.ts.map +1 -1
  150. package/build-types/dispatch.d.ts +4 -0
  151. package/build-types/dispatch.d.ts.map +1 -1
  152. package/build-types/factory.d.ts +2 -1
  153. package/build-types/factory.d.ts.map +1 -1
  154. package/build-types/index.d.ts.map +1 -1
  155. package/build-types/plugins/index.d.ts +1 -1
  156. package/build-types/plugins/index.d.ts.map +1 -1
  157. package/build-types/plugins/persistence/index.d.ts +38 -27
  158. package/build-types/plugins/persistence/index.d.ts.map +1 -1
  159. package/build-types/plugins/persistence/storage/default.d.ts +7 -1
  160. package/build-types/plugins/persistence/storage/default.d.ts.map +1 -1
  161. package/build-types/plugins/persistence/storage/object.d.ts +4 -5
  162. package/build-types/plugins/persistence/storage/object.d.ts.map +1 -1
  163. package/build-types/promise-middleware.d.ts +6 -4
  164. package/build-types/promise-middleware.d.ts.map +1 -1
  165. package/build-types/redux-store/index.d.ts +12 -15
  166. package/build-types/redux-store/index.d.ts.map +1 -1
  167. package/build-types/redux-store/metadata/selectors.d.ts +45 -50
  168. package/build-types/redux-store/metadata/selectors.d.ts.map +1 -1
  169. package/build-types/redux-store/metadata/types.d.ts +48 -0
  170. package/build-types/redux-store/metadata/types.d.ts.map +1 -0
  171. package/build-types/registry.d.ts +5 -49
  172. package/build-types/registry.d.ts.map +1 -1
  173. package/build-types/resolvers-cache-middleware.d.ts +13 -7
  174. package/build-types/resolvers-cache-middleware.d.ts.map +1 -1
  175. package/build-types/select.d.ts +3 -0
  176. package/build-types/select.d.ts.map +1 -1
  177. package/build-types/store/index.d.ts +5 -12
  178. package/build-types/store/index.d.ts.map +1 -1
  179. package/build-types/types.d.ts +201 -10
  180. package/build-types/types.d.ts.map +1 -1
  181. package/package.json +9 -9
  182. package/src/components/async-mode-provider/{context.js → context.ts} +0 -1
  183. package/src/components/async-mode-provider/{use-async-mode.js → use-async-mode.ts} +1 -1
  184. package/src/components/registry-provider/{use-registry.js → use-registry.ts} +3 -2
  185. package/src/components/use-dispatch/{use-dispatch-with-map.js → use-dispatch-with-map.ts} +18 -9
  186. package/src/components/use-dispatch/{use-dispatch.js → use-dispatch.ts} +25 -20
  187. package/src/components/use-select/{index.js → index.ts} +84 -70
  188. package/src/components/with-dispatch/{index.js → index.tsx} +20 -13
  189. package/src/components/with-registry/{index.js → index.tsx} +1 -5
  190. package/src/components/with-select/{index.js → index.tsx} +17 -10
  191. package/src/{controls.js → controls.ts} +53 -26
  192. package/src/{default-registry.js → default-registry.ts} +3 -1
  193. package/src/dispatch.ts +7 -1
  194. package/src/factory.ts +4 -3
  195. package/src/index.ts +4 -8
  196. package/src/plugins/persistence/{index.js → index.ts} +93 -54
  197. package/src/plugins/persistence/storage/{default.js → default.ts} +3 -2
  198. package/src/plugins/persistence/storage/object.ts +25 -0
  199. package/src/{promise-middleware.js → promise-middleware.ts} +3 -3
  200. package/src/redux-store/{index.js → index.ts} +218 -131
  201. package/src/redux-store/metadata/{selectors.js → selectors.ts} +83 -54
  202. package/src/redux-store/metadata/types.ts +100 -0
  203. package/src/{registry.js → registry.ts} +98 -88
  204. package/src/{resolvers-cache-middleware.js → resolvers-cache-middleware.ts} +22 -7
  205. package/src/select.ts +6 -1
  206. package/src/store/{index.js → index.ts} +13 -8
  207. package/src/types.ts +263 -24
  208. package/src/plugins/persistence/storage/object.js +0 -23
  209. /package/src/components/async-mode-provider/{index.js → index.ts} +0 -0
  210. /package/src/components/registry-provider/{context.js → context.ts} +0 -0
  211. /package/src/components/registry-provider/{index.js → index.ts} +0 -0
  212. /package/src/components/use-dispatch/{index.js → index.ts} +0 -0
  213. /package/src/plugins/{index.js → index.ts} +0 -0
@@ -16,10 +16,21 @@ import { isShallowEqual } from '@wordpress/is-shallow-equal';
16
16
  */
17
17
  import useRegistry from '../registry-provider/use-registry';
18
18
  import useAsyncMode from '../async-mode-provider/use-async-mode';
19
+ import type {
20
+ MapSelect,
21
+ SelectFunction,
22
+ StoreDescriptor,
23
+ AnyConfig,
24
+ UseSelectReturn,
25
+ DataRegistry,
26
+ } from '../../types';
19
27
 
20
28
  const renderQueue = createQueue();
21
29
 
22
- function warnOnUnstableReference( a, b ) {
30
+ function warnOnUnstableReference(
31
+ a: Record< string, unknown >,
32
+ b: Record< string, unknown >
33
+ ): void {
23
34
  if ( ! a || ! b ) {
24
35
  return;
25
36
  }
@@ -38,41 +49,32 @@ function warnOnUnstableReference( a, b ) {
38
49
  );
39
50
  }
40
51
 
41
- /**
42
- * @typedef {import('../../types').StoreDescriptor<C>} StoreDescriptor
43
- * @template {import('../../types').AnyConfig} C
44
- */
45
- /**
46
- * @typedef {import('../../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig
47
- * @template State
48
- * @template {Record<string,import('../../types').ActionCreator>} Actions
49
- * @template Selectors
50
- */
51
- /** @typedef {import('../../types').MapSelect} MapSelect */
52
- /**
53
- * @typedef {import('../../types').UseSelectReturn<T>} UseSelectReturn
54
- * @template {MapSelect|StoreDescriptor<any>} T
55
- */
52
+ interface StoreSubscriber {
53
+ subscribe: ( listener: () => void ) => () => void;
54
+ updateStores: ( newStores: string[] ) => void;
55
+ }
56
56
 
57
- function Store( registry, suspense ) {
58
- const select = suspense ? registry.suspendSelect : registry.select;
57
+ function Store( registry: DataRegistry, suspense: boolean ) {
58
+ const select = ( suspense
59
+ ? registry.suspendSelect
60
+ : registry.select ) as unknown as SelectFunction;
59
61
  const queueContext = {};
60
- let lastMapSelect;
61
- let lastMapResult;
62
+ let lastMapSelect: MapSelect | undefined;
63
+ let lastMapResult: unknown;
62
64
  let lastMapResultValid = false;
63
- let lastIsAsync;
64
- let subscriber;
65
- let didWarnUnstableReference;
66
- const storeStatesOnMount = new Map();
65
+ let lastIsAsync: boolean | undefined;
66
+ let subscriber: StoreSubscriber | undefined;
67
+ let didWarnUnstableReference: boolean | undefined;
68
+ const storeStatesOnMount = new Map< string, unknown >();
67
69
 
68
- function getStoreState( name ) {
70
+ function getStoreState( name: string ): unknown {
69
71
  // If there's no store property (custom generic store), return an empty
70
72
  // object. When comparing the state, the empty objects will cause the
71
73
  // equality check to fail, setting `lastMapResultValid` to false.
72
74
  return registry.stores[ name ]?.store?.getState?.() ?? {};
73
75
  }
74
76
 
75
- const createSubscriber = ( stores ) => {
77
+ const createSubscriber = ( stores: string[] ): StoreSubscriber => {
76
78
  // The set of stores the `subscribe` function is supposed to subscribe to. Here it is
77
79
  // initialized, and then the `updateStores` function can add new stores to it.
78
80
  const activeStores = [ ...stores ];
@@ -80,9 +82,9 @@ function Store( registry, suspense ) {
80
82
  // The `subscribe` function, which is passed to the `useSyncExternalStore` hook, could
81
83
  // be called multiple times to establish multiple subscriptions. That's why we need to
82
84
  // keep a set of active subscriptions;
83
- const activeSubscriptions = new Set();
85
+ const activeSubscriptions = new Set< ( storeName: string ) => void >();
84
86
 
85
- function subscribe( listener ) {
87
+ function subscribe( listener: () => void ): () => void {
86
88
  // Maybe invalidate the value right after subscription was created.
87
89
  // React will call `getValue` after subscribing, to detect store
88
90
  // updates that happened in the interval between the `getValue` call
@@ -116,8 +118,8 @@ function Store( registry, suspense ) {
116
118
  }
117
119
  };
118
120
 
119
- const unsubs = [];
120
- function subscribeStore( storeName ) {
121
+ const unsubs: Array< VoidFunction > = [];
122
+ function subscribeStore( storeName: string ) {
121
123
  unsubs.push( registry.subscribe( onChange, storeName ) );
122
124
  }
123
125
 
@@ -140,7 +142,7 @@ function Store( registry, suspense ) {
140
142
  }
141
143
 
142
144
  // Check if `newStores` contains some stores we're not subscribed to yet, and add them.
143
- function updateStores( newStores ) {
145
+ function updateStores( newStores: string[] ) {
144
146
  for ( const newStore of newStores ) {
145
147
  if ( activeStores.includes( newStore ) ) {
146
148
  continue;
@@ -159,22 +161,22 @@ function Store( registry, suspense ) {
159
161
  return { subscribe, updateStores };
160
162
  };
161
163
 
162
- return ( mapSelect, isAsync ) => {
163
- function updateValue() {
164
+ return ( mapSelect: MapSelect, isAsync: boolean ) => {
165
+ function updateValue(): void {
164
166
  // If the last value is valid, and the `mapSelect` callback hasn't changed,
165
167
  // then we can safely return the cached value. The value can change only on
166
168
  // store update, and in that case value will be invalidated by the listener.
167
169
  if ( lastMapResultValid && mapSelect === lastMapSelect ) {
168
- return lastMapResult;
170
+ return;
169
171
  }
170
172
 
171
- const listeningStores = { current: null };
173
+ const listeningStores = { current: null as string[] | null };
172
174
  const mapResult = registry.__unstableMarkListeningStores(
173
175
  () => mapSelect( select, registry ),
174
176
  listeningStores
175
177
  );
176
178
 
177
- if ( globalThis.SCRIPT_DEBUG ) {
179
+ if ( ( globalThis as any ).SCRIPT_DEBUG ) {
178
180
  if ( ! didWarnUnstableReference ) {
179
181
  const secondMapResult = mapSelect( select, registry );
180
182
  if ( ! isShallowEqual( mapResult, secondMapResult ) ) {
@@ -185,12 +187,12 @@ function Store( registry, suspense ) {
185
187
  }
186
188
 
187
189
  if ( ! subscriber ) {
188
- for ( const name of listeningStores.current ) {
190
+ for ( const name of listeningStores.current! ) {
189
191
  storeStatesOnMount.set( name, getStoreState( name ) );
190
192
  }
191
- subscriber = createSubscriber( listeningStores.current );
193
+ subscriber = createSubscriber( listeningStores.current! );
192
194
  } else {
193
- subscriber.updateStores( listeningStores.current );
195
+ subscriber.updateStores( listeningStores.current! );
194
196
  }
195
197
 
196
198
  // If the new value is shallow-equal to the old one, keep the old one so
@@ -221,15 +223,19 @@ function Store( registry, suspense ) {
221
223
  lastIsAsync = isAsync;
222
224
 
223
225
  // Return a pair of functions that can be passed to `useSyncExternalStore`.
224
- return { subscribe: subscriber.subscribe, getValue };
226
+ return { subscribe: subscriber!.subscribe, getValue };
225
227
  };
226
228
  }
227
229
 
228
- function _useStaticSelect( storeName ) {
230
+ function _useStaticSelect( storeName: StoreDescriptor< AnyConfig > | string ) {
229
231
  return useRegistry().select( storeName );
230
232
  }
231
233
 
232
- function _useMappingSelect( suspense, mapSelect, deps ) {
234
+ function _useMappingSelect(
235
+ suspense: boolean,
236
+ mapSelect: MapSelect,
237
+ deps: unknown[]
238
+ ) {
233
239
  const registry = useRegistry();
234
240
  const isAsync = useAsyncMode();
235
241
  const store = useMemo(
@@ -239,6 +245,7 @@ function _useMappingSelect( suspense, mapSelect, deps ) {
239
245
 
240
246
  // These are "pass-through" dependencies from the parent hook,
241
247
  // and the parent should catch any hook rule violations.
248
+ // eslint-disable-next-line react-hooks/exhaustive-deps
242
249
  const selector = useCallback( mapSelect, deps );
243
250
  const { subscribe, getValue } = store( selector, isAsync );
244
251
  const result = useSyncExternalStore( subscribe, getValue, getValue );
@@ -252,16 +259,15 @@ function _useMappingSelect( suspense, mapSelect, deps ) {
252
259
  * In general, this custom React hook follows the
253
260
  * [rules of hooks](https://react.dev/reference/rules/rules-of-hooks).
254
261
  *
255
- * @template {MapSelect | StoreDescriptor<any>} T
256
- * @param {T} mapSelect Function called on every state change. The returned value is
257
- * exposed to the component implementing this hook. The function
258
- * receives the `registry.select` method on the first argument
259
- * and the `registry` on the second argument.
260
- * When a store key is passed, all selectors for the store will be
261
- * returned. This is only meant for usage of these selectors in event
262
- * callbacks, not for data needed to create the element tree.
263
- * @param {unknown[]=} deps If provided, this memoizes the mapSelect so the same `mapSelect` is
264
- * invoked on every state change unless the dependencies change.
262
+ * @param mapSelect Function called on every state change. The returned value is
263
+ * exposed to the component implementing this hook. The function
264
+ * receives the `registry.select` method on the first argument
265
+ * and the `registry` on the second argument.
266
+ * When a store key is passed, all selectors for the store will be
267
+ * returned. This is only meant for usage of these selectors in event
268
+ * callbacks, not for data needed to create the element tree.
269
+ * @param deps If provided, this memoizes the mapSelect so the same `mapSelect` is
270
+ * invoked on every state change unless the dependencies change.
265
271
  *
266
272
  * @example
267
273
  * ```js
@@ -308,9 +314,12 @@ function _useMappingSelect( suspense, mapSelect, deps ) {
308
314
  * return <div onPaste={ onPaste }>{ children }</div>;
309
315
  * }
310
316
  * ```
311
- * @return {UseSelectReturn<T>} A custom react hook.
317
+ *
318
+ * @return The selected data or store selectors.
312
319
  */
313
- export default function useSelect( mapSelect, deps ) {
320
+ export default function useSelect<
321
+ T extends MapSelect | StoreDescriptor< AnyConfig >,
322
+ >( mapSelect: T, deps?: unknown[] ): UseSelectReturn< T > {
314
323
  // On initial call, on mount, determine the mode of this `useSelect` call
315
324
  // and then never allow it to change on subsequent updates.
316
325
  const staticSelectMode = typeof mapSelect !== 'function';
@@ -326,30 +335,35 @@ export default function useSelect( mapSelect, deps ) {
326
335
 
327
336
  // `staticSelectMode` is not allowed to change during the hook instance's,
328
337
  // lifetime, so the rules of hooks are not really violated.
329
- return staticSelectMode
330
- ? _useStaticSelect( mapSelect )
331
- : _useMappingSelect( false, mapSelect, deps );
338
+
339
+ return (
340
+ staticSelectMode
341
+ ? _useStaticSelect( mapSelect as StoreDescriptor< AnyConfig > )
342
+ : _useMappingSelect( false, mapSelect as MapSelect, deps! )
343
+ ) as UseSelectReturn< T >;
332
344
  }
333
345
 
334
346
  /**
335
347
  * A variant of the `useSelect` hook that has the same API, but is a compatible
336
348
  * Suspense-enabled data source.
337
349
  *
338
- * @template {MapSelect} T
339
- * @param {T} mapSelect Function called on every state change. The
340
- * returned value is exposed to the component
341
- * using this hook. The function receives the
342
- * `registry.suspendSelect` method as the first
343
- * argument and the `registry` as the second one.
344
- * @param {Array} deps A dependency array used to memoize the `mapSelect`
345
- * so that the same `mapSelect` is invoked on every
346
- * state change unless the dependencies change.
350
+ * @param mapSelect Function called on every state change. The
351
+ * returned value is exposed to the component
352
+ * using this hook. The function receives the
353
+ * `registry.suspendSelect` method as the first
354
+ * argument and the `registry` as the second one.
355
+ * @param deps A dependency array used to memoize the `mapSelect`
356
+ * so that the same `mapSelect` is invoked on every
357
+ * state change unless the dependencies change.
347
358
  *
348
- * @throws {Promise} A suspense Promise that is thrown if any of the called
359
+ * @throws A suspense Promise that is thrown if any of the called
349
360
  * selectors is in an unresolved state.
350
361
  *
351
- * @return {ReturnType<T>} Data object returned by the `mapSelect` function.
362
+ * @return Data object returned by the `mapSelect` function.
352
363
  */
353
- export function useSuspenseSelect( mapSelect, deps ) {
354
- return _useMappingSelect( true, mapSelect, deps );
364
+ export function useSuspenseSelect< T extends MapSelect >(
365
+ mapSelect: T,
366
+ deps: unknown[]
367
+ ): ReturnType< T > {
368
+ return _useMappingSelect( true, mapSelect, deps ) as ReturnType< T >;
355
369
  }
@@ -7,19 +7,18 @@ import { createHigherOrderComponent } from '@wordpress/compose';
7
7
  * Internal dependencies
8
8
  */
9
9
  import { useDispatchWithMap } from '../use-dispatch';
10
-
11
- /** @typedef {React.ComponentType} ComponentType */
10
+ import type { DataRegistry } from '../../types';
12
11
 
13
12
  /**
14
13
  * Higher-order component used to add dispatch props using registered action
15
14
  * creators.
16
15
  *
17
- * @param {Function} mapDispatchToProps A function of returning an object of
18
- * prop names where value is a
19
- * dispatch-bound action creator, or a
20
- * function to be called with the
21
- * component's props and returning an
22
- * action creator.
16
+ * @param mapDispatchToProps A function of returning an object of
17
+ * prop names where value is a
18
+ * dispatch-bound action creator, or a
19
+ * function to be called with the
20
+ * component's props and returning an
21
+ * action creator.
23
22
  *
24
23
  * @example
25
24
  * ```jsx
@@ -89,13 +88,21 @@ import { useDispatchWithMap } from '../use-dispatch';
89
88
  * returns an object with the same keys. For example, it should not contain
90
89
  * conditions under which a different value would be returned.
91
90
  *
92
- * @return {ComponentType} Enhanced component with merged dispatcher props.
91
+ * @return Enhanced component with merged dispatcher props.
93
92
  */
94
- const withDispatch = ( mapDispatchToProps ) =>
93
+ const withDispatch = (
94
+ mapDispatchToProps: (
95
+ dispatch: DataRegistry[ 'dispatch' ],
96
+ ownProps: Record< string, unknown >,
97
+ registry: DataRegistry
98
+ ) => Record< string, ( ...args: unknown[] ) => unknown >
99
+ ) =>
95
100
  createHigherOrderComponent(
96
- ( WrappedComponent ) => ( ownProps ) => {
97
- const mapDispatch = ( dispatch, registry ) =>
98
- mapDispatchToProps( dispatch, ownProps, registry );
101
+ ( WrappedComponent ) => ( ownProps: Record< string, unknown > ) => {
102
+ const mapDispatch = (
103
+ dispatch: DataRegistry[ 'dispatch' ],
104
+ registry: DataRegistry
105
+ ) => mapDispatchToProps( dispatch, ownProps, registry );
99
106
  const dispatchProps = useDispatchWithMap( mapDispatch, [] );
100
107
  return <WrappedComponent { ...ownProps } { ...dispatchProps } />;
101
108
  },
@@ -11,13 +11,9 @@ import { RegistryConsumer } from '../registry-provider';
11
11
  /**
12
12
  * Higher-order component which renders the original component with the current
13
13
  * registry context passed as its `registry` prop.
14
- *
15
- * @param {Component} OriginalComponent Original component.
16
- *
17
- * @return {Component} Enhanced component.
18
14
  */
19
15
  const withRegistry = createHigherOrderComponent(
20
- ( OriginalComponent ) => ( props ) => (
16
+ ( OriginalComponent ) => ( props: Record< string, unknown > ) => (
21
17
  <RegistryConsumer>
22
18
  { ( registry ) => (
23
19
  <OriginalComponent { ...props } registry={ registry } />
@@ -7,16 +7,15 @@ import { createHigherOrderComponent, pure } from '@wordpress/compose';
7
7
  * Internal dependencies
8
8
  */
9
9
  import useSelect from '../use-select';
10
-
11
- /** @typedef {React.ComponentType} ComponentType */
10
+ import type { SelectFunction, DataRegistry } from '../../types';
12
11
 
13
12
  /**
14
13
  * Higher-order component used to inject state-derived props using registered
15
14
  * selectors.
16
15
  *
17
- * @param {Function} mapSelectToProps Function called on every state change,
18
- * expected to return object of props to
19
- * merge with the component's own props.
16
+ * @param mapSelectToProps Function called on every state change,
17
+ * expected to return object of props to
18
+ * merge with the component's own props.
20
19
  *
21
20
  * @example
22
21
  * ```js
@@ -48,14 +47,22 @@ import useSelect from '../use-select';
48
47
  * component and update automatically if the price of a hammer ever changes in
49
48
  * the store.
50
49
  *
51
- * @return {ComponentType} Enhanced component with merged state data props.
50
+ * @return Enhanced component with merged state data props.
52
51
  */
53
- const withSelect = ( mapSelectToProps ) =>
52
+ const withSelect = (
53
+ mapSelectToProps: (
54
+ select: SelectFunction,
55
+ ownProps: Record< string, unknown >,
56
+ registry: DataRegistry
57
+ ) => Record< string, unknown >
58
+ ) =>
54
59
  createHigherOrderComponent(
55
60
  ( WrappedComponent ) =>
56
- pure( ( ownProps ) => {
57
- const mapSelect = ( select, registry ) =>
58
- mapSelectToProps( select, ownProps, registry );
61
+ pure( ( ownProps: Record< string, unknown > ) => {
62
+ const mapSelect = (
63
+ select: SelectFunction,
64
+ registry: DataRegistry
65
+ ) => mapSelectToProps( select, ownProps, registry );
59
66
  const mergeProps = useSelect( mapSelect );
60
67
  return <WrappedComponent { ...ownProps } { ...mergeProps } />;
61
68
  } ),
@@ -2,14 +2,13 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import { createRegistryControl } from './factory';
5
-
6
- /** @typedef {import('./types').StoreDescriptor} StoreDescriptor */
5
+ import type { StoreDescriptor } from './types';
7
6
 
8
7
  const SELECT = '@@data/SELECT';
9
8
  const RESOLVE_SELECT = '@@data/RESOLVE_SELECT';
10
9
  const DISPATCH = '@@data/DISPATCH';
11
10
 
12
- function isObject( object ) {
11
+ function isStoreDescriptor( object: unknown ): object is StoreDescriptor {
13
12
  return object !== null && typeof object === 'object';
14
13
  }
15
14
 
@@ -19,9 +18,9 @@ function isObject( object ) {
19
18
  * Note: This control synchronously returns the current selector value, triggering the
20
19
  * resolution, but not waiting for it.
21
20
  *
22
- * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
23
- * @param {string} selectorName The name of the selector.
24
- * @param {Array} args Arguments for the selector.
21
+ * @param storeNameOrDescriptor Unique namespace identifier for the store
22
+ * @param selectorName The name of the selector.
23
+ * @param args Arguments for the selector.
25
24
  *
26
25
  * @example
27
26
  * ```js
@@ -34,12 +33,16 @@ function isObject( object ) {
34
33
  * }
35
34
  * ```
36
35
  *
37
- * @return {Object} The control descriptor.
36
+ * @return The control descriptor.
38
37
  */
39
- function select( storeNameOrDescriptor, selectorName, ...args ) {
38
+ function select(
39
+ storeNameOrDescriptor: string | StoreDescriptor,
40
+ selectorName: string,
41
+ ...args: unknown[]
42
+ ) {
40
43
  return {
41
44
  type: SELECT,
42
- storeKey: isObject( storeNameOrDescriptor )
45
+ storeKey: isStoreDescriptor( storeNameOrDescriptor )
43
46
  ? storeNameOrDescriptor.name
44
47
  : storeNameOrDescriptor,
45
48
  selectorName,
@@ -54,9 +57,9 @@ function select( storeNameOrDescriptor, selectorName, ...args ) {
54
57
  * selectors that may have a resolver. In such case, it will return a `Promise` that resolves
55
58
  * after the selector finishes resolving, with the final result value.
56
59
  *
57
- * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
58
- * @param {string} selectorName The name of the selector
59
- * @param {Array} args Arguments for the selector.
60
+ * @param storeNameOrDescriptor Unique namespace identifier for the store
61
+ * @param selectorName The name of the selector
62
+ * @param args Arguments for the selector.
60
63
  *
61
64
  * @example
62
65
  * ```js
@@ -69,12 +72,16 @@ function select( storeNameOrDescriptor, selectorName, ...args ) {
69
72
  * }
70
73
  * ```
71
74
  *
72
- * @return {Object} The control descriptor.
75
+ * @return The control descriptor.
73
76
  */
74
- function resolveSelect( storeNameOrDescriptor, selectorName, ...args ) {
77
+ function resolveSelect(
78
+ storeNameOrDescriptor: string | StoreDescriptor< any >,
79
+ selectorName: string,
80
+ ...args: any[]
81
+ ) {
75
82
  return {
76
83
  type: RESOLVE_SELECT,
77
- storeKey: isObject( storeNameOrDescriptor )
84
+ storeKey: isStoreDescriptor( storeNameOrDescriptor )
78
85
  ? storeNameOrDescriptor.name
79
86
  : storeNameOrDescriptor,
80
87
  selectorName,
@@ -85,9 +92,9 @@ function resolveSelect( storeNameOrDescriptor, selectorName, ...args ) {
85
92
  /**
86
93
  * Dispatches a control action for triggering a registry dispatch.
87
94
  *
88
- * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
89
- * @param {string} actionName The name of the action to dispatch
90
- * @param {Array} args Arguments for the dispatch action.
95
+ * @param storeNameOrDescriptor Unique namespace identifier for the store
96
+ * @param actionName The name of the action to dispatch
97
+ * @param args Arguments for the dispatch action.
91
98
  *
92
99
  * @example
93
100
  * ```js
@@ -100,12 +107,16 @@ function resolveSelect( storeNameOrDescriptor, selectorName, ...args ) {
100
107
  * }
101
108
  * ```
102
109
  *
103
- * @return {Object} The control descriptor.
110
+ * @return The control descriptor.
104
111
  */
105
- function dispatch( storeNameOrDescriptor, actionName, ...args ) {
112
+ function dispatch(
113
+ storeNameOrDescriptor: string | StoreDescriptor,
114
+ actionName: string,
115
+ ...args: unknown[]
116
+ ) {
106
117
  return {
107
118
  type: DISPATCH,
108
- storeKey: isObject( storeNameOrDescriptor )
119
+ storeKey: isStoreDescriptor( storeNameOrDescriptor )
109
120
  ? storeNameOrDescriptor.name
110
121
  : storeNameOrDescriptor,
111
122
  actionName,
@@ -115,17 +126,33 @@ function dispatch( storeNameOrDescriptor, actionName, ...args ) {
115
126
 
116
127
  export const controls = { select, resolveSelect, dispatch };
117
128
 
129
+ type SelectorControlArgs = {
130
+ storeKey: string;
131
+ selectorName: string;
132
+ args: unknown[];
133
+ };
134
+
135
+ type ActionControlArgs = {
136
+ storeKey: string;
137
+ actionName: string;
138
+ args: unknown[];
139
+ };
140
+
118
141
  export const builtinControls = {
119
142
  [ SELECT ]: createRegistryControl(
120
143
  ( registry ) =>
121
- ( { storeKey, selectorName, args } ) =>
144
+ ( { storeKey, selectorName, args }: SelectorControlArgs ) =>
122
145
  registry.select( storeKey )[ selectorName ]( ...args )
123
146
  ),
124
147
  [ RESOLVE_SELECT ]: createRegistryControl(
125
148
  ( registry ) =>
126
- ( { storeKey, selectorName, args } ) => {
127
- const method = registry.select( storeKey )[ selectorName ]
128
- .hasResolver
149
+ ( { storeKey, selectorName, args }: SelectorControlArgs ) => {
150
+ const selector = registry.select( storeKey )[
151
+ selectorName
152
+ ] as ( ( ...a: any[] ) => any ) & {
153
+ hasResolver?: boolean;
154
+ };
155
+ const method = selector.hasResolver
129
156
  ? 'resolveSelect'
130
157
  : 'select';
131
158
  return registry[ method ]( storeKey )[ selectorName ](
@@ -135,7 +162,7 @@ export const builtinControls = {
135
162
  ),
136
163
  [ DISPATCH ]: createRegistryControl(
137
164
  ( registry ) =>
138
- ( { storeKey, actionName, args } ) =>
165
+ ( { storeKey, actionName, args }: ActionControlArgs ) =>
139
166
  registry.dispatch( storeKey )[ actionName ]( ...args )
140
167
  ),
141
168
  };
@@ -3,4 +3,6 @@
3
3
  */
4
4
  import { createRegistry } from './registry';
5
5
 
6
- export default createRegistry();
6
+ const defaultRegistry = createRegistry();
7
+
8
+ export default defaultRegistry;
package/src/dispatch.ts CHANGED
@@ -11,6 +11,10 @@ import defaultRegistry from './default-registry';
11
11
  * Note: Action creators returned by the dispatch will return a promise when
12
12
  * they are called.
13
13
  *
14
+ * Warning: This global `dispatch` function only works with the default registry.
15
+ * It will not work with custom `RegistryProvider` or `BlockEditorProvider` contexts.
16
+ * In React components, prefer the `useDispatch` hook instead, which is registry-aware.
17
+ *
14
18
  * @param storeNameOrDescriptor The store descriptor. The legacy calling convention of passing
15
19
  * the store name is also supported.
16
20
  *
@@ -28,5 +32,7 @@ export function dispatch<
28
32
  >(
29
33
  storeNameOrDescriptor: StoreNameOrDescriptor
30
34
  ): DispatchReturn< StoreNameOrDescriptor > {
31
- return defaultRegistry.dispatch( storeNameOrDescriptor );
35
+ return defaultRegistry.dispatch(
36
+ storeNameOrDescriptor
37
+ ) as DispatchReturn< StoreNameOrDescriptor >;
32
38
  }
package/src/factory.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import type { select as globalSelect } from './select';
5
+ import type { DataRegistry } from './types';
5
6
 
6
7
  type RegistrySelector< Selector extends ( ...args: any[] ) => any > = {
7
8
  ( ...args: Parameters< Selector > ): ReturnType< Selector >;
@@ -101,9 +102,9 @@ export function createRegistrySelector<
101
102
  *
102
103
  * @return Registry control that can be registered with a store.
103
104
  */
104
- export function createRegistryControl< T extends ( ...args: any ) => any >(
105
- registryControl: T & { isRegistryControl?: boolean }
106
- ) {
105
+ export function createRegistryControl<
106
+ T extends ( registry: DataRegistry ) => ( ...args: any ) => any,
107
+ >( registryControl: T & { isRegistryControl?: boolean } ) {
107
108
  registryControl.isRegistryControl = true;
108
109
 
109
110
  return registryControl;
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import defaultRegistryUntyped from './default-registry';
4
+ import defaultRegistry from './default-registry';
5
5
  import * as plugins from './plugins';
6
6
  import { combineReducers as combineReducersModule } from './redux-store';
7
7
 
@@ -13,12 +13,6 @@ import type {
13
13
  CurriedSelectorsResolveOf,
14
14
  } from './types';
15
15
 
16
- // The runtime registry is created from the JavaScript implementation in `registry.js`.
17
- // Its JSDoc type (`WPDataRegistry`) doesn't include some newer methods like
18
- // `resolveSelect` or `suspendSelect`, so we widen the type here for the typed
19
- // exports in this module.
20
- const defaultRegistry: any = defaultRegistryUntyped;
21
-
22
16
  export { default as withSelect } from './components/with-select';
23
17
  export { default as withDispatch } from './components/with-dispatch';
24
18
  export { default as withRegistry } from './components/with-registry';
@@ -115,7 +109,9 @@ export const combineReducers =
115
109
  export function resolveSelect< T extends StoreDescriptor< AnyConfig > >(
116
110
  storeNameOrDescriptor: T | string
117
111
  ): CurriedSelectorsResolveOf< T > {
118
- return defaultRegistry.resolveSelect( storeNameOrDescriptor );
112
+ return defaultRegistry.resolveSelect(
113
+ storeNameOrDescriptor
114
+ ) as CurriedSelectorsResolveOf< T >;
119
115
  }
120
116
 
121
117
  /**