@wordpress/data 10.41.0 → 10.41.1-next.v.202603102151.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
|
@@ -10,53 +10,38 @@ import createReduxStore from './redux-store';
|
|
|
10
10
|
import coreDataStore from './store';
|
|
11
11
|
import { createEmitter } from './utils/emitter';
|
|
12
12
|
import { lock, unlock } from './lock-unlock';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* store.
|
|
25
|
-
* @property {Function} subscribe Given a function callback, invokes
|
|
26
|
-
* the callback on any change to state
|
|
27
|
-
* within any registered store.
|
|
28
|
-
* @property {Function} select Given a namespace key, returns an
|
|
29
|
-
* object of the store's registered
|
|
30
|
-
* selectors.
|
|
31
|
-
* @property {Function} dispatch Given a namespace key, returns an
|
|
32
|
-
* object of the store's registered
|
|
33
|
-
* action dispatchers.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @typedef {Object} WPDataPlugin An object of registry function overrides.
|
|
38
|
-
*
|
|
39
|
-
* @property {Function} registerStore registers store.
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
function getStoreName( storeNameOrDescriptor ) {
|
|
13
|
+
import type {
|
|
14
|
+
StoreDescriptor,
|
|
15
|
+
StoreNameOrDescriptor,
|
|
16
|
+
DataRegistry,
|
|
17
|
+
DataPlugin,
|
|
18
|
+
InternalStoreInstance,
|
|
19
|
+
AnyConfig,
|
|
20
|
+
ReduxStoreConfig,
|
|
21
|
+
} from './types';
|
|
22
|
+
|
|
23
|
+
function getStoreName( storeNameOrDescriptor: StoreNameOrDescriptor ): string {
|
|
43
24
|
return typeof storeNameOrDescriptor === 'string'
|
|
44
25
|
? storeNameOrDescriptor
|
|
45
26
|
: storeNameOrDescriptor.name;
|
|
46
27
|
}
|
|
28
|
+
|
|
47
29
|
/**
|
|
48
30
|
* Creates a new store registry, given an optional object of initial store
|
|
49
31
|
* configurations.
|
|
50
32
|
*
|
|
51
|
-
* @param
|
|
52
|
-
* @param
|
|
33
|
+
* @param storeConfigs Initial store configurations.
|
|
34
|
+
* @param parent Parent registry.
|
|
53
35
|
*
|
|
54
|
-
* @return
|
|
36
|
+
* @return Data registry.
|
|
55
37
|
*/
|
|
56
|
-
export function createRegistry(
|
|
57
|
-
|
|
38
|
+
export function createRegistry(
|
|
39
|
+
storeConfigs: Record< string, ReduxStoreConfig< any, any, any > > = {},
|
|
40
|
+
parent: DataRegistry | null = null
|
|
41
|
+
): DataRegistry {
|
|
42
|
+
const stores: Record< string, InternalStoreInstance > = {};
|
|
58
43
|
const emitter = createEmitter();
|
|
59
|
-
let listeningStores = null;
|
|
44
|
+
let listeningStores: Set< string > | null = null;
|
|
60
45
|
|
|
61
46
|
/**
|
|
62
47
|
* Global listener called for each store's update.
|
|
@@ -69,12 +54,15 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
69
54
|
* Subscribe to changes to any data, either in all stores in registry, or
|
|
70
55
|
* in one specific store.
|
|
71
56
|
*
|
|
72
|
-
* @param
|
|
73
|
-
* @param
|
|
57
|
+
* @param listener Listener function.
|
|
58
|
+
* @param storeNameOrDescriptor Optional store name.
|
|
74
59
|
*
|
|
75
|
-
* @return
|
|
60
|
+
* @return Unsubscribe function.
|
|
76
61
|
*/
|
|
77
|
-
const subscribe = (
|
|
62
|
+
const subscribe = (
|
|
63
|
+
listener: () => void,
|
|
64
|
+
storeNameOrDescriptor?: StoreNameOrDescriptor
|
|
65
|
+
): ( () => void ) => {
|
|
78
66
|
// subscribe to all stores
|
|
79
67
|
if ( ! storeNameOrDescriptor ) {
|
|
80
68
|
return emitter.subscribe( listener );
|
|
@@ -101,12 +89,12 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
101
89
|
/**
|
|
102
90
|
* Calls a selector given the current state and extra arguments.
|
|
103
91
|
*
|
|
104
|
-
* @param
|
|
105
|
-
*
|
|
92
|
+
* @param storeNameOrDescriptor Unique namespace identifier for the store
|
|
93
|
+
* or the store descriptor.
|
|
106
94
|
*
|
|
107
|
-
* @return
|
|
95
|
+
* @return The selector's returned value.
|
|
108
96
|
*/
|
|
109
|
-
function select( storeNameOrDescriptor ) {
|
|
97
|
+
function select( storeNameOrDescriptor: StoreNameOrDescriptor ) {
|
|
110
98
|
const storeName = getStoreName( storeNameOrDescriptor );
|
|
111
99
|
listeningStores?.add( storeName );
|
|
112
100
|
const store = stores[ storeName ];
|
|
@@ -117,7 +105,11 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
117
105
|
return parent?.select( storeName );
|
|
118
106
|
}
|
|
119
107
|
|
|
120
|
-
function __unstableMarkListeningStores
|
|
108
|
+
function __unstableMarkListeningStores< T >(
|
|
109
|
+
this: DataRegistry,
|
|
110
|
+
callback: () => T,
|
|
111
|
+
ref: { current: string[] | null }
|
|
112
|
+
): T {
|
|
121
113
|
listeningStores = new Set();
|
|
122
114
|
try {
|
|
123
115
|
return callback.call( this );
|
|
@@ -132,18 +124,18 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
132
124
|
* state so that you only need to supply additional arguments, and modified so that they return
|
|
133
125
|
* promises that resolve to their eventual values, after any resolvers have ran.
|
|
134
126
|
*
|
|
135
|
-
* @param
|
|
136
|
-
*
|
|
137
|
-
*
|
|
127
|
+
* @param storeNameOrDescriptor The store descriptor. The legacy calling
|
|
128
|
+
* convention of passing the store name is
|
|
129
|
+
* also supported.
|
|
138
130
|
*
|
|
139
|
-
* @return
|
|
131
|
+
* @return Each key of the object matches the name of a selector.
|
|
140
132
|
*/
|
|
141
|
-
function resolveSelect( storeNameOrDescriptor ) {
|
|
133
|
+
function resolveSelect( storeNameOrDescriptor: StoreNameOrDescriptor ) {
|
|
142
134
|
const storeName = getStoreName( storeNameOrDescriptor );
|
|
143
135
|
listeningStores?.add( storeName );
|
|
144
136
|
const store = stores[ storeName ];
|
|
145
137
|
if ( store ) {
|
|
146
|
-
return store.getResolveSelectors();
|
|
138
|
+
return store.getResolveSelectors!();
|
|
147
139
|
}
|
|
148
140
|
|
|
149
141
|
return parent && parent.resolveSelect( storeName );
|
|
@@ -154,18 +146,18 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
154
146
|
* state so that you only need to supply additional arguments, and modified so that they throw
|
|
155
147
|
* promises in case the selector is not resolved yet.
|
|
156
148
|
*
|
|
157
|
-
* @param
|
|
158
|
-
*
|
|
159
|
-
*
|
|
149
|
+
* @param storeNameOrDescriptor The store descriptor. The legacy calling
|
|
150
|
+
* convention of passing the store name is
|
|
151
|
+
* also supported.
|
|
160
152
|
*
|
|
161
|
-
* @return
|
|
153
|
+
* @return Object containing the store's suspense-wrapped selectors.
|
|
162
154
|
*/
|
|
163
|
-
function suspendSelect( storeNameOrDescriptor ) {
|
|
155
|
+
function suspendSelect( storeNameOrDescriptor: StoreNameOrDescriptor ) {
|
|
164
156
|
const storeName = getStoreName( storeNameOrDescriptor );
|
|
165
157
|
listeningStores?.add( storeName );
|
|
166
158
|
const store = stores[ storeName ];
|
|
167
159
|
if ( store ) {
|
|
168
|
-
return store.getSuspendSelectors();
|
|
160
|
+
return store.getSuspendSelectors!();
|
|
169
161
|
}
|
|
170
162
|
|
|
171
163
|
return parent && parent.suspendSelect( storeName );
|
|
@@ -174,12 +166,12 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
174
166
|
/**
|
|
175
167
|
* Returns the available actions for a part of the state.
|
|
176
168
|
*
|
|
177
|
-
* @param
|
|
178
|
-
*
|
|
169
|
+
* @param storeNameOrDescriptor Unique namespace identifier for the store
|
|
170
|
+
* or the store descriptor.
|
|
179
171
|
*
|
|
180
|
-
* @return
|
|
172
|
+
* @return The action's returned value.
|
|
181
173
|
*/
|
|
182
|
-
function dispatch( storeNameOrDescriptor ) {
|
|
174
|
+
function dispatch( storeNameOrDescriptor: StoreNameOrDescriptor ) {
|
|
183
175
|
const storeName = getStoreName( storeNameOrDescriptor );
|
|
184
176
|
const store = stores[ storeName ];
|
|
185
177
|
if ( store ) {
|
|
@@ -192,7 +184,9 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
192
184
|
//
|
|
193
185
|
// Deprecated
|
|
194
186
|
// TODO: Remove this after `use()` is removed.
|
|
195
|
-
function withPlugins(
|
|
187
|
+
function withPlugins(
|
|
188
|
+
attributes: Record< string, unknown >
|
|
189
|
+
): Record< string, unknown > {
|
|
196
190
|
return Object.fromEntries(
|
|
197
191
|
Object.entries( attributes ).map( ( [ key, attribute ] ) => {
|
|
198
192
|
if ( typeof attribute !== 'function' ) {
|
|
@@ -200,8 +194,8 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
200
194
|
}
|
|
201
195
|
return [
|
|
202
196
|
key,
|
|
203
|
-
|
|
204
|
-
return registry[ key ]
|
|
197
|
+
( ...args: unknown[] ) => {
|
|
198
|
+
return ( registry as any )[ key ]( ...args );
|
|
205
199
|
},
|
|
206
200
|
];
|
|
207
201
|
} )
|
|
@@ -211,17 +205,20 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
211
205
|
/**
|
|
212
206
|
* Registers a store instance.
|
|
213
207
|
*
|
|
214
|
-
* @param
|
|
215
|
-
* @param
|
|
208
|
+
* @param name Store registry name.
|
|
209
|
+
* @param createStoreFunc Function that creates a store object (getSelectors, getActions, subscribe).
|
|
216
210
|
*/
|
|
217
|
-
function registerStoreInstance(
|
|
211
|
+
function registerStoreInstance(
|
|
212
|
+
name: string,
|
|
213
|
+
createStoreFunc: () => InternalStoreInstance
|
|
214
|
+
): InternalStoreInstance {
|
|
218
215
|
if ( stores[ name ] ) {
|
|
219
216
|
// eslint-disable-next-line no-console
|
|
220
217
|
console.error( 'Store "' + name + '" is already registered.' );
|
|
221
218
|
return stores[ name ];
|
|
222
219
|
}
|
|
223
220
|
|
|
224
|
-
const store =
|
|
221
|
+
const store: any = createStoreFunc();
|
|
225
222
|
|
|
226
223
|
if ( typeof store.getSelectors !== 'function' ) {
|
|
227
224
|
throw new TypeError( 'store.getSelectors must be a function' );
|
|
@@ -237,7 +234,7 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
237
234
|
// pending listeners.
|
|
238
235
|
store.emitter = createEmitter();
|
|
239
236
|
const currentSubscribe = store.subscribe;
|
|
240
|
-
store.subscribe = ( listener ) => {
|
|
237
|
+
store.subscribe = ( listener: () => void ) => {
|
|
241
238
|
const unsubscribeFromEmitter = store.emitter.subscribe( listener );
|
|
242
239
|
const unsubscribeFromStore = currentSubscribe( () => {
|
|
243
240
|
if ( store.emitter.isPaused ) {
|
|
@@ -277,15 +274,19 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
277
274
|
/**
|
|
278
275
|
* Registers a new store given a store descriptor.
|
|
279
276
|
*
|
|
280
|
-
* @param
|
|
277
|
+
* @param store Store descriptor.
|
|
281
278
|
*/
|
|
282
|
-
function register( store ) {
|
|
283
|
-
registerStoreInstance(
|
|
284
|
-
store.
|
|
279
|
+
function register( store: StoreDescriptor< AnyConfig > ) {
|
|
280
|
+
registerStoreInstance(
|
|
281
|
+
store.name,
|
|
282
|
+
() => store.instantiate( registry ) as InternalStoreInstance
|
|
285
283
|
);
|
|
286
284
|
}
|
|
287
285
|
|
|
288
|
-
function registerGenericStore(
|
|
286
|
+
function registerGenericStore(
|
|
287
|
+
name: string,
|
|
288
|
+
store: InternalStoreInstance
|
|
289
|
+
) {
|
|
289
290
|
deprecated( 'wp.data.registerGenericStore', {
|
|
290
291
|
since: '5.9',
|
|
291
292
|
alternative: 'wp.data.register( storeDescriptor )',
|
|
@@ -296,24 +297,31 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
296
297
|
/**
|
|
297
298
|
* Registers a standard `@wordpress/data` store.
|
|
298
299
|
*
|
|
299
|
-
* @param
|
|
300
|
-
* @param
|
|
300
|
+
* @param storeName Unique namespace identifier.
|
|
301
|
+
* @param options Store description (reducer, actions, selectors, resolvers).
|
|
301
302
|
*
|
|
302
|
-
* @return
|
|
303
|
+
* @return Registered store object.
|
|
303
304
|
*/
|
|
304
|
-
function registerStore(
|
|
305
|
+
function registerStore(
|
|
306
|
+
storeName: string,
|
|
307
|
+
options: ReduxStoreConfig< any, any, any >
|
|
308
|
+
) {
|
|
305
309
|
if ( ! options.reducer ) {
|
|
306
310
|
throw new TypeError( 'Must specify store reducer' );
|
|
307
311
|
}
|
|
308
312
|
|
|
309
|
-
const store = registerStoreInstance(
|
|
310
|
-
|
|
313
|
+
const store = registerStoreInstance(
|
|
314
|
+
storeName,
|
|
315
|
+
() =>
|
|
316
|
+
createReduxStore( storeName, options ).instantiate(
|
|
317
|
+
registry
|
|
318
|
+
) as InternalStoreInstance
|
|
311
319
|
);
|
|
312
320
|
|
|
313
321
|
return store.store;
|
|
314
322
|
}
|
|
315
323
|
|
|
316
|
-
function batch( callback ) {
|
|
324
|
+
function batch( callback: () => void ) {
|
|
317
325
|
// If we're already batching, just call the callback.
|
|
318
326
|
if ( emitter.isPaused ) {
|
|
319
327
|
callback();
|
|
@@ -332,7 +340,7 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
332
340
|
}
|
|
333
341
|
}
|
|
334
342
|
|
|
335
|
-
let registry = {
|
|
343
|
+
let registry: DataRegistry = {
|
|
336
344
|
batch,
|
|
337
345
|
stores,
|
|
338
346
|
namespaces: stores, // TODO: Deprecate/remove this.
|
|
@@ -346,12 +354,12 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
346
354
|
registerGenericStore,
|
|
347
355
|
registerStore,
|
|
348
356
|
__unstableMarkListeningStores,
|
|
349
|
-
};
|
|
357
|
+
} as DataRegistry;
|
|
350
358
|
|
|
351
359
|
//
|
|
352
360
|
// TODO:
|
|
353
361
|
// This function will be deprecated as soon as it is no longer internally referenced.
|
|
354
|
-
function use( plugin, options ) {
|
|
362
|
+
function use( plugin: DataPlugin, options?: Record< string, unknown > ) {
|
|
355
363
|
if ( ! plugin ) {
|
|
356
364
|
return;
|
|
357
365
|
}
|
|
@@ -374,9 +382,11 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
374
382
|
parent.subscribe( globalListener );
|
|
375
383
|
}
|
|
376
384
|
|
|
377
|
-
const registryWithPlugins = withPlugins(
|
|
385
|
+
const registryWithPlugins = withPlugins(
|
|
386
|
+
registry as unknown as Record< string, unknown >
|
|
387
|
+
);
|
|
378
388
|
lock( registryWithPlugins, {
|
|
379
|
-
privateActionsOf: ( name ) => {
|
|
389
|
+
privateActionsOf: ( name: string ) => {
|
|
380
390
|
try {
|
|
381
391
|
return unlock( stores[ name ].store ).privateActions;
|
|
382
392
|
} catch ( e ) {
|
|
@@ -385,7 +395,7 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
385
395
|
return {};
|
|
386
396
|
}
|
|
387
397
|
},
|
|
388
|
-
privateSelectorsOf: ( name ) => {
|
|
398
|
+
privateSelectorsOf: ( name: string ) => {
|
|
389
399
|
try {
|
|
390
400
|
return unlock( stores[ name ].store ).privateSelectors;
|
|
391
401
|
} catch ( e ) {
|
|
@@ -393,5 +403,5 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
393
403
|
}
|
|
394
404
|
},
|
|
395
405
|
} );
|
|
396
|
-
return registryWithPlugins;
|
|
406
|
+
return registryWithPlugins as unknown as DataRegistry;
|
|
397
407
|
}
|
|
@@ -1,23 +1,38 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Middleware } from 'redux';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { DataRegistry } from './types';
|
|
2
10
|
|
|
3
11
|
/**
|
|
4
12
|
* Creates a middleware handling resolvers cache invalidation.
|
|
5
13
|
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
14
|
+
* @param registry Registry for which to create the middleware.
|
|
15
|
+
* @param storeName Name of the store for which to create the middleware.
|
|
8
16
|
*
|
|
9
|
-
* @return
|
|
17
|
+
* @return Middleware function.
|
|
10
18
|
*/
|
|
11
19
|
const createResolversCacheMiddleware =
|
|
12
|
-
( registry, storeName
|
|
20
|
+
( registry: DataRegistry, storeName: string ): Middleware =>
|
|
21
|
+
() =>
|
|
22
|
+
( next ) =>
|
|
23
|
+
( action ) => {
|
|
13
24
|
const resolvers = registry.select( storeName ).getCachedResolvers();
|
|
14
|
-
const resolverEntries =
|
|
25
|
+
const resolverEntries =
|
|
26
|
+
Object.entries< Map< string, { status: 'finished' | 'error' } > >(
|
|
27
|
+
resolvers
|
|
28
|
+
);
|
|
15
29
|
resolverEntries.forEach( ( [ selectorName, resolversByArgs ] ) => {
|
|
16
30
|
const resolver =
|
|
17
31
|
registry.stores[ storeName ]?.resolvers?.[ selectorName ];
|
|
18
32
|
if ( ! resolver || ! resolver.shouldInvalidate ) {
|
|
19
33
|
return;
|
|
20
34
|
}
|
|
35
|
+
const { shouldInvalidate } = resolver;
|
|
21
36
|
resolversByArgs.forEach( ( value, args ) => {
|
|
22
37
|
// Works around a bug in `EquivalentKeyMap` where `map.delete` merely sets an entry value
|
|
23
38
|
// to `undefined` and `map.forEach` then iterates also over these orphaned entries.
|
|
@@ -32,7 +47,7 @@ const createResolversCacheMiddleware =
|
|
|
32
47
|
return;
|
|
33
48
|
}
|
|
34
49
|
|
|
35
|
-
if ( !
|
|
50
|
+
if ( ! shouldInvalidate( action, ...args ) ) {
|
|
36
51
|
return;
|
|
37
52
|
}
|
|
38
53
|
|
package/src/select.ts
CHANGED
|
@@ -9,6 +9,9 @@ import defaultRegistry from './default-registry';
|
|
|
9
9
|
* The selector functions are been pre-bound to pass the current state automatically.
|
|
10
10
|
* As a consumer, you need only pass arguments of the selector, if applicable.
|
|
11
11
|
*
|
|
12
|
+
* Warning: This global `select` function only works with the default registry.
|
|
13
|
+
* It will not work with custom `RegistryProvider` or `BlockEditorProvider` contexts.
|
|
14
|
+
* In React components, prefer the `useSelect` hook instead, which is registry-aware.
|
|
12
15
|
*
|
|
13
16
|
* @param storeNameOrDescriptor The store descriptor. The legacy calling convention
|
|
14
17
|
* of passing the store name is also supported.
|
|
@@ -26,5 +29,7 @@ import defaultRegistry from './default-registry';
|
|
|
26
29
|
export function select< T extends StoreDescriptor< AnyConfig > >(
|
|
27
30
|
storeNameOrDescriptor: string | T
|
|
28
31
|
): CurriedSelectorsOf< T > {
|
|
29
|
-
return defaultRegistry.select(
|
|
32
|
+
return defaultRegistry.select(
|
|
33
|
+
storeNameOrDescriptor
|
|
34
|
+
) as CurriedSelectorsOf< T >;
|
|
30
35
|
}
|
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { DataRegistry, StoreDescriptor, AnyConfig } from '../types';
|
|
5
|
+
|
|
6
|
+
const coreDataStore: StoreDescriptor< AnyConfig > = {
|
|
2
7
|
name: 'core/data',
|
|
3
|
-
instantiate( registry ) {
|
|
8
|
+
instantiate( registry: DataRegistry ) {
|
|
4
9
|
const getCoreDataSelector =
|
|
5
|
-
( selectorName ) =>
|
|
6
|
-
( key, ...args ) => {
|
|
10
|
+
( selectorName: string ) =>
|
|
11
|
+
( key: string, ...args: unknown[] ) => {
|
|
7
12
|
return registry.select( key )[ selectorName ]( ...args );
|
|
8
13
|
};
|
|
9
14
|
|
|
10
15
|
const getCoreDataAction =
|
|
11
|
-
( actionName ) =>
|
|
12
|
-
( key, ...args ) => {
|
|
16
|
+
( actionName: string ) =>
|
|
17
|
+
( key: string, ...args: unknown[] ) => {
|
|
13
18
|
return registry.dispatch( key )[ actionName ]( ...args );
|
|
14
19
|
};
|
|
15
20
|
|
|
16
21
|
return {
|
|
17
|
-
getSelectors() {
|
|
22
|
+
getSelectors(): Record< string, ( ...args: any[] ) => any > {
|
|
18
23
|
return Object.fromEntries(
|
|
19
24
|
[
|
|
20
25
|
'getIsResolving',
|
|
@@ -29,7 +34,7 @@ const coreDataStore = {
|
|
|
29
34
|
);
|
|
30
35
|
},
|
|
31
36
|
|
|
32
|
-
getActions() {
|
|
37
|
+
getActions(): Record< string, ( ...args: any[] ) => any > {
|
|
33
38
|
return Object.fromEntries(
|
|
34
39
|
[
|
|
35
40
|
'startResolution',
|