@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.
- package/README.md +20 -28
- package/build/components/async-mode-provider/context.cjs +1 -1
- package/build/components/async-mode-provider/context.cjs.map +3 -3
- package/build/components/async-mode-provider/index.cjs +1 -1
- package/build/components/async-mode-provider/index.cjs.map +1 -1
- package/build/components/async-mode-provider/use-async-mode.cjs +1 -1
- package/build/components/async-mode-provider/use-async-mode.cjs.map +3 -3
- package/build/components/registry-provider/context.cjs +1 -1
- package/build/components/registry-provider/context.cjs.map +1 -1
- package/build/components/registry-provider/index.cjs +1 -1
- package/build/components/registry-provider/index.cjs.map +1 -1
- package/build/components/registry-provider/use-registry.cjs +1 -1
- package/build/components/registry-provider/use-registry.cjs.map +3 -3
- package/build/components/use-dispatch/index.cjs +1 -1
- package/build/components/use-dispatch/index.cjs.map +1 -1
- package/build/components/use-dispatch/use-dispatch-with-map.cjs +1 -1
- package/build/components/use-dispatch/use-dispatch-with-map.cjs.map +3 -3
- package/build/components/use-dispatch/use-dispatch.cjs +1 -1
- package/build/components/use-dispatch/use-dispatch.cjs.map +3 -3
- package/build/components/use-select/index.cjs +2 -2
- package/build/components/use-select/index.cjs.map +3 -3
- package/build/components/with-dispatch/index.cjs +1 -1
- package/build/components/with-dispatch/index.cjs.map +3 -3
- package/build/components/with-registry/index.cjs +1 -1
- package/build/components/with-registry/index.cjs.map +3 -3
- package/build/components/with-select/index.cjs +1 -1
- package/build/components/with-select/index.cjs.map +3 -3
- package/build/controls.cjs +7 -6
- package/build/controls.cjs.map +3 -3
- package/build/default-registry.cjs +3 -2
- package/build/default-registry.cjs.map +3 -3
- package/build/dispatch.cjs +3 -1
- package/build/dispatch.cjs.map +2 -2
- package/build/factory.cjs.map +2 -2
- package/build/index.cjs +9 -8
- package/build/index.cjs.map +3 -3
- package/build/plugins/index.cjs +1 -1
- package/build/plugins/index.cjs.map +1 -1
- package/build/plugins/persistence/index.cjs +13 -8
- package/build/plugins/persistence/index.cjs.map +3 -3
- package/build/plugins/persistence/storage/default.cjs +1 -1
- package/build/plugins/persistence/storage/default.cjs.map +3 -3
- package/build/plugins/persistence/storage/object.cjs +1 -1
- package/build/plugins/persistence/storage/object.cjs.map +3 -3
- package/build/promise-middleware.cjs +2 -1
- package/build/promise-middleware.cjs.map +3 -3
- package/build/redux-store/index.cjs +32 -16
- package/build/redux-store/index.cjs.map +3 -3
- package/build/redux-store/metadata/selectors.cjs +1 -1
- package/build/redux-store/metadata/selectors.cjs.map +3 -3
- package/build/redux-store/metadata/types.cjs +19 -0
- package/build/redux-store/metadata/types.cjs.map +7 -0
- package/build/registry.cjs +11 -7
- package/build/registry.cjs.map +3 -3
- package/build/resolvers-cache-middleware.cjs +6 -3
- package/build/resolvers-cache-middleware.cjs.map +3 -3
- package/build/select.cjs +3 -1
- package/build/select.cjs.map +2 -2
- package/build/store/index.cjs +1 -1
- package/build/store/index.cjs.map +3 -3
- package/build/types.cjs.map +1 -1
- package/build-module/components/async-mode-provider/context.mjs +1 -1
- package/build-module/components/async-mode-provider/context.mjs.map +3 -3
- package/build-module/components/async-mode-provider/index.mjs +1 -1
- package/build-module/components/async-mode-provider/index.mjs.map +1 -1
- package/build-module/components/async-mode-provider/use-async-mode.mjs +1 -1
- package/build-module/components/async-mode-provider/use-async-mode.mjs.map +3 -3
- package/build-module/components/registry-provider/context.mjs +1 -1
- package/build-module/components/registry-provider/context.mjs.map +1 -1
- package/build-module/components/registry-provider/index.mjs +1 -1
- package/build-module/components/registry-provider/index.mjs.map +1 -1
- package/build-module/components/registry-provider/use-registry.mjs +1 -1
- package/build-module/components/registry-provider/use-registry.mjs.map +3 -3
- package/build-module/components/use-dispatch/index.mjs +1 -1
- package/build-module/components/use-dispatch/index.mjs.map +1 -1
- package/build-module/components/use-dispatch/use-dispatch-with-map.mjs +1 -1
- package/build-module/components/use-dispatch/use-dispatch-with-map.mjs.map +3 -3
- package/build-module/components/use-dispatch/use-dispatch.mjs +1 -1
- package/build-module/components/use-dispatch/use-dispatch.mjs.map +3 -3
- package/build-module/components/use-select/index.mjs +2 -2
- package/build-module/components/use-select/index.mjs.map +3 -3
- package/build-module/components/with-dispatch/index.mjs +1 -1
- package/build-module/components/with-dispatch/index.mjs.map +3 -3
- package/build-module/components/with-registry/index.mjs +1 -1
- package/build-module/components/with-registry/index.mjs.map +3 -3
- package/build-module/components/with-select/index.mjs +1 -1
- package/build-module/components/with-select/index.mjs.map +3 -3
- package/build-module/controls.mjs +7 -6
- package/build-module/controls.mjs.map +3 -3
- package/build-module/default-registry.mjs +3 -2
- package/build-module/default-registry.mjs.map +3 -3
- package/build-module/dispatch.mjs +3 -1
- package/build-module/dispatch.mjs.map +2 -2
- package/build-module/factory.mjs.map +2 -2
- package/build-module/index.mjs +4 -3
- package/build-module/index.mjs.map +2 -2
- package/build-module/plugins/index.mjs +1 -1
- package/build-module/plugins/index.mjs.map +1 -1
- package/build-module/plugins/persistence/index.mjs +13 -8
- package/build-module/plugins/persistence/index.mjs.map +3 -3
- package/build-module/plugins/persistence/storage/default.mjs +1 -1
- package/build-module/plugins/persistence/storage/default.mjs.map +3 -3
- package/build-module/plugins/persistence/storage/object.mjs +1 -1
- package/build-module/plugins/persistence/storage/object.mjs.map +3 -3
- package/build-module/promise-middleware.mjs +2 -1
- package/build-module/promise-middleware.mjs.map +3 -3
- package/build-module/redux-store/index.mjs +32 -16
- package/build-module/redux-store/index.mjs.map +3 -3
- package/build-module/redux-store/metadata/selectors.mjs +1 -1
- package/build-module/redux-store/metadata/selectors.mjs.map +3 -3
- package/build-module/redux-store/metadata/types.mjs +1 -0
- package/build-module/redux-store/metadata/types.mjs.map +7 -0
- package/build-module/registry.mjs +11 -7
- package/build-module/registry.mjs.map +3 -3
- package/build-module/resolvers-cache-middleware.mjs +6 -3
- package/build-module/resolvers-cache-middleware.mjs.map +3 -3
- package/build-module/select.mjs +3 -1
- package/build-module/select.mjs.map +2 -2
- package/build-module/store/index.mjs +1 -1
- package/build-module/store/index.mjs.map +3 -3
- package/build-types/components/async-mode-provider/context.d.ts +37 -3
- package/build-types/components/async-mode-provider/context.d.ts.map +1 -1
- package/build-types/components/async-mode-provider/index.d.ts +2 -2
- package/build-types/components/async-mode-provider/index.d.ts.map +1 -1
- package/build-types/components/async-mode-provider/use-async-mode.d.ts.map +1 -1
- package/build-types/components/registry-provider/context.d.ts +10 -3
- package/build-types/components/registry-provider/context.d.ts.map +1 -1
- package/build-types/components/registry-provider/index.d.ts +2 -2
- package/build-types/components/registry-provider/index.d.ts.map +1 -1
- package/build-types/components/registry-provider/use-registry.d.ts +3 -2
- package/build-types/components/registry-provider/use-registry.d.ts.map +1 -1
- package/build-types/components/use-dispatch/index.d.ts +2 -2
- package/build-types/components/use-dispatch/index.d.ts.map +1 -1
- package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts +11 -9
- package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts.map +1 -1
- package/build-types/components/use-dispatch/use-dispatch.d.ts +10 -19
- package/build-types/components/use-dispatch/use-dispatch.d.ts.map +1 -1
- package/build-types/components/use-select/index.d.ts +24 -28
- package/build-types/components/use-select/index.d.ts.map +1 -1
- package/build-types/components/with-dispatch/index.d.ts +10 -11
- package/build-types/components/with-dispatch/index.d.ts.map +1 -1
- package/build-types/components/with-registry/index.d.ts +2 -6
- package/build-types/components/with-registry/index.d.ts.map +1 -1
- package/build-types/components/with-select/index.d.ts +7 -8
- package/build-types/components/with-select/index.d.ts.map +1 -1
- package/build-types/controls.d.ts +57 -44
- package/build-types/controls.d.ts.map +1 -1
- package/build-types/default-registry.d.ts +2 -2
- package/build-types/default-registry.d.ts.map +1 -1
- package/build-types/dispatch.d.ts +4 -0
- package/build-types/dispatch.d.ts.map +1 -1
- package/build-types/factory.d.ts +2 -1
- package/build-types/factory.d.ts.map +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/plugins/index.d.ts +1 -1
- package/build-types/plugins/index.d.ts.map +1 -1
- package/build-types/plugins/persistence/index.d.ts +38 -27
- package/build-types/plugins/persistence/index.d.ts.map +1 -1
- package/build-types/plugins/persistence/storage/default.d.ts +7 -1
- package/build-types/plugins/persistence/storage/default.d.ts.map +1 -1
- package/build-types/plugins/persistence/storage/object.d.ts +4 -5
- package/build-types/plugins/persistence/storage/object.d.ts.map +1 -1
- package/build-types/promise-middleware.d.ts +6 -4
- package/build-types/promise-middleware.d.ts.map +1 -1
- package/build-types/redux-store/index.d.ts +12 -15
- package/build-types/redux-store/index.d.ts.map +1 -1
- package/build-types/redux-store/metadata/selectors.d.ts +45 -50
- package/build-types/redux-store/metadata/selectors.d.ts.map +1 -1
- package/build-types/redux-store/metadata/types.d.ts +48 -0
- package/build-types/redux-store/metadata/types.d.ts.map +1 -0
- package/build-types/registry.d.ts +5 -49
- package/build-types/registry.d.ts.map +1 -1
- package/build-types/resolvers-cache-middleware.d.ts +13 -7
- package/build-types/resolvers-cache-middleware.d.ts.map +1 -1
- package/build-types/select.d.ts +3 -0
- package/build-types/select.d.ts.map +1 -1
- package/build-types/store/index.d.ts +5 -12
- package/build-types/store/index.d.ts.map +1 -1
- package/build-types/types.d.ts +201 -10
- package/build-types/types.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/components/async-mode-provider/{context.js → context.ts} +0 -1
- package/src/components/async-mode-provider/{use-async-mode.js → use-async-mode.ts} +1 -1
- package/src/components/registry-provider/{use-registry.js → use-registry.ts} +3 -2
- package/src/components/use-dispatch/{use-dispatch-with-map.js → use-dispatch-with-map.ts} +18 -9
- package/src/components/use-dispatch/{use-dispatch.js → use-dispatch.ts} +25 -20
- package/src/components/use-select/{index.js → index.ts} +84 -70
- package/src/components/with-dispatch/{index.js → index.tsx} +20 -13
- package/src/components/with-registry/{index.js → index.tsx} +1 -5
- package/src/components/with-select/{index.js → index.tsx} +17 -10
- package/src/{controls.js → controls.ts} +53 -26
- package/src/{default-registry.js → default-registry.ts} +3 -1
- package/src/dispatch.ts +7 -1
- package/src/factory.ts +4 -3
- package/src/index.ts +4 -8
- package/src/plugins/persistence/{index.js → index.ts} +93 -54
- package/src/plugins/persistence/storage/{default.js → default.ts} +3 -2
- package/src/plugins/persistence/storage/object.ts +25 -0
- package/src/{promise-middleware.js → promise-middleware.ts} +3 -3
- package/src/redux-store/{index.js → index.ts} +218 -131
- package/src/redux-store/metadata/{selectors.js → selectors.ts} +83 -54
- package/src/redux-store/metadata/types.ts +100 -0
- package/src/{registry.js → registry.ts} +98 -88
- package/src/{resolvers-cache-middleware.js → resolvers-cache-middleware.ts} +22 -7
- package/src/select.ts +6 -1
- package/src/store/{index.js → index.ts} +13 -8
- package/src/types.ts +263 -24
- package/src/plugins/persistence/storage/object.js +0 -23
- /package/src/components/async-mode-provider/{index.js → index.ts} +0 -0
- /package/src/components/registry-provider/{context.js → context.ts} +0 -0
- /package/src/components/registry-provider/{index.js → index.ts} +0 -0
- /package/src/components/use-dispatch/{index.js → index.ts} +0 -0
- /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(
|
|
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
|
-
|
|
43
|
-
|
|
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
|
|
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
|
|
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
|
|
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(
|
|
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
|
-
* @
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
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
|
-
*
|
|
317
|
+
*
|
|
318
|
+
* @return The selected data or store selectors.
|
|
312
319
|
*/
|
|
313
|
-
export default function useSelect
|
|
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
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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
|
-
* @
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
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
|
|
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
|
|
362
|
+
* @return Data object returned by the `mapSelect` function.
|
|
352
363
|
*/
|
|
353
|
-
export function useSuspenseSelect
|
|
354
|
-
|
|
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
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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
|
|
91
|
+
* @return Enhanced component with merged dispatcher props.
|
|
93
92
|
*/
|
|
94
|
-
const withDispatch = (
|
|
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 = (
|
|
98
|
-
|
|
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
|
|
18
|
-
*
|
|
19
|
-
*
|
|
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
|
|
50
|
+
* @return Enhanced component with merged state data props.
|
|
52
51
|
*/
|
|
53
|
-
const withSelect = (
|
|
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 = (
|
|
58
|
-
|
|
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
|
|
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
|
|
23
|
-
* @param
|
|
24
|
-
* @param
|
|
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
|
|
36
|
+
* @return The control descriptor.
|
|
38
37
|
*/
|
|
39
|
-
function select(
|
|
38
|
+
function select(
|
|
39
|
+
storeNameOrDescriptor: string | StoreDescriptor,
|
|
40
|
+
selectorName: string,
|
|
41
|
+
...args: unknown[]
|
|
42
|
+
) {
|
|
40
43
|
return {
|
|
41
44
|
type: SELECT,
|
|
42
|
-
storeKey:
|
|
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
|
|
58
|
-
* @param
|
|
59
|
-
* @param
|
|
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
|
|
75
|
+
* @return The control descriptor.
|
|
73
76
|
*/
|
|
74
|
-
function resolveSelect(
|
|
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:
|
|
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
|
|
89
|
-
* @param
|
|
90
|
-
* @param
|
|
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
|
|
110
|
+
* @return The control descriptor.
|
|
104
111
|
*/
|
|
105
|
-
function dispatch(
|
|
112
|
+
function dispatch(
|
|
113
|
+
storeNameOrDescriptor: string | StoreDescriptor,
|
|
114
|
+
actionName: string,
|
|
115
|
+
...args: unknown[]
|
|
116
|
+
) {
|
|
106
117
|
return {
|
|
107
118
|
type: DISPATCH,
|
|
108
|
-
storeKey:
|
|
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
|
|
128
|
-
|
|
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
|
};
|
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(
|
|
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<
|
|
105
|
-
|
|
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
|
|
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(
|
|
112
|
+
return defaultRegistry.resolveSelect(
|
|
113
|
+
storeNameOrDescriptor
|
|
114
|
+
) as CurriedSelectorsResolveOf< T >;
|
|
119
115
|
}
|
|
120
116
|
|
|
121
117
|
/**
|