@wordpress/data 6.1.4 → 6.2.1
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/CHANGELOG.md +18 -0
- package/LICENSE.md +1 -1
- package/README.md +77 -71
- package/build/components/use-dispatch/use-dispatch.js +8 -8
- package/build/components/use-dispatch/use-dispatch.js.map +1 -1
- package/build/components/use-select/index.js +58 -43
- package/build/components/use-select/index.js.map +1 -1
- package/build/controls.js +16 -16
- package/build/controls.js.map +1 -1
- package/build/index.js +14 -14
- package/build/index.js.map +1 -1
- package/build/plugins/index.js +0 -8
- package/build/plugins/index.js.map +1 -1
- package/build/redux-store/index.js +15 -15
- package/build/redux-store/index.js.map +1 -1
- package/build/registry.js +82 -68
- package/build/registry.js.map +1 -1
- package/build/resolvers-cache-middleware.js +5 -3
- package/build/resolvers-cache-middleware.js.map +1 -1
- package/build/store/index.js +42 -43
- package/build/store/index.js.map +1 -1
- package/build/utils/emitter.js +1 -1
- package/build/utils/emitter.js.map +1 -1
- package/build-module/components/use-dispatch/use-dispatch.js +8 -8
- package/build-module/components/use-dispatch/use-dispatch.js.map +1 -1
- package/build-module/components/use-select/index.js +60 -44
- package/build-module/components/use-select/index.js.map +1 -1
- package/build-module/controls.js +16 -16
- package/build-module/controls.js.map +1 -1
- package/build-module/index.js +14 -14
- package/build-module/index.js.map +1 -1
- package/build-module/plugins/index.js +0 -1
- package/build-module/plugins/index.js.map +1 -1
- package/build-module/redux-store/index.js +15 -15
- package/build-module/redux-store/index.js.map +1 -1
- package/build-module/registry.js +82 -68
- package/build-module/registry.js.map +1 -1
- package/build-module/resolvers-cache-middleware.js +3 -3
- package/build-module/resolvers-cache-middleware.js.map +1 -1
- package/build-module/store/index.js +42 -42
- package/build-module/store/index.js.map +1 -1
- package/build-module/utils/emitter.js +1 -1
- package/build-module/utils/emitter.js.map +1 -1
- package/build-types/types.d.ts +29 -16
- package/build-types/types.d.ts.map +1 -1
- package/build-types/utils/emitter.d.ts +2 -2
- package/build-types/utils/emitter.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/components/use-dispatch/test/use-dispatch.js +1 -1
- package/src/components/use-dispatch/use-dispatch.js +9 -9
- package/src/components/use-select/index.js +61 -46
- package/src/components/use-select/test/index.js +32 -32
- package/src/controls.js +22 -22
- package/src/index.js +14 -14
- package/src/plugins/index.js +0 -1
- package/src/redux-store/index.js +15 -15
- package/src/registry.js +88 -75
- package/src/resolvers-cache-middleware.js +3 -3
- package/src/store/index.js +49 -48
- package/src/test/registry.js +33 -47
- package/src/types.ts +44 -16
- package/src/utils/emitter.js +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/plugins/controls/index.js +0 -24
- package/build/plugins/controls/index.js.map +0 -1
- package/build/store/name.js +0 -15
- package/build/store/name.js.map +0 -1
- package/build-module/plugins/controls/index.js +0 -12
- package/build-module/plugins/controls/index.js.map +0 -1
- package/build-module/store/name.js +0 -7
- package/build-module/store/name.js.map +0 -1
- package/src/plugins/controls/index.js +0 -12
- package/src/store/name.js +0 -6
package/src/plugins/index.js
CHANGED
package/src/redux-store/index.js
CHANGED
|
@@ -22,9 +22,9 @@ import metadataReducer from './metadata/reducer';
|
|
|
22
22
|
import * as metadataSelectors from './metadata/selectors';
|
|
23
23
|
import * as metadataActions from './metadata/actions';
|
|
24
24
|
|
|
25
|
-
/** @typedef {import('../types').
|
|
26
|
-
/** @typedef {import('../types').
|
|
27
|
-
/** @typedef {import('../types').
|
|
25
|
+
/** @typedef {import('../types').DataRegistry} DataRegistry */
|
|
26
|
+
/** @typedef {import('../types').StoreDescriptor} StoreDescriptor */
|
|
27
|
+
/** @typedef {import('../types').ReduxStoreConfig} ReduxStoreConfig */
|
|
28
28
|
|
|
29
29
|
const trimUndefinedValues = ( array ) => {
|
|
30
30
|
const result = [ ...array ];
|
|
@@ -68,7 +68,7 @@ function createResolversCache() {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
|
-
* Creates a data store
|
|
71
|
+
* Creates a data store descriptor for the provided Redux store configuration containing
|
|
72
72
|
* properties describing reducer, actions, selectors, controls and resolvers.
|
|
73
73
|
*
|
|
74
74
|
* @example
|
|
@@ -83,12 +83,12 @@ function createResolversCache() {
|
|
|
83
83
|
* } );
|
|
84
84
|
* ```
|
|
85
85
|
*
|
|
86
|
-
* @param {string}
|
|
87
|
-
* @param {
|
|
88
|
-
*
|
|
89
|
-
*
|
|
86
|
+
* @param {string} key Unique namespace identifier.
|
|
87
|
+
* @param {ReduxStoreConfig} options Registered store options, with properties
|
|
88
|
+
* describing reducer, actions, selectors,
|
|
89
|
+
* and resolvers.
|
|
90
90
|
*
|
|
91
|
-
* @return {
|
|
91
|
+
* @return {StoreDescriptor} Store Object.
|
|
92
92
|
*/
|
|
93
93
|
export default function createReduxStore( key, options ) {
|
|
94
94
|
return {
|
|
@@ -210,12 +210,12 @@ export default function createReduxStore( key, options ) {
|
|
|
210
210
|
/**
|
|
211
211
|
* Creates a redux store for a namespace.
|
|
212
212
|
*
|
|
213
|
-
* @param {string}
|
|
214
|
-
* @param {Object}
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* @param {
|
|
218
|
-
* @param {Object}
|
|
213
|
+
* @param {string} key Unique namespace identifier.
|
|
214
|
+
* @param {Object} options Registered store options, with properties
|
|
215
|
+
* describing reducer, actions, selectors,
|
|
216
|
+
* and resolvers.
|
|
217
|
+
* @param {DataRegistry} registry Registry reference.
|
|
218
|
+
* @param {Object} thunkArgs Argument object for the thunk middleware.
|
|
219
219
|
* @return {Object} Newly created redux store.
|
|
220
220
|
*/
|
|
221
221
|
function instantiateReduxStore( key, options, registry, thunkArgs ) {
|
package/src/registry.js
CHANGED
|
@@ -3,15 +3,19 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { mapValues, isObject, forEach } from 'lodash';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import deprecated from '@wordpress/deprecated';
|
|
10
|
+
|
|
6
11
|
/**
|
|
7
12
|
* Internal dependencies
|
|
8
13
|
*/
|
|
9
14
|
import createReduxStore from './redux-store';
|
|
10
|
-
import
|
|
11
|
-
import { STORE_NAME } from './store/name';
|
|
15
|
+
import coreDataStore from './store';
|
|
12
16
|
import { createEmitter } from './utils/emitter';
|
|
13
17
|
|
|
14
|
-
/** @typedef {import('./types').
|
|
18
|
+
/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */
|
|
15
19
|
|
|
16
20
|
/**
|
|
17
21
|
* @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.
|
|
@@ -74,15 +78,15 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
74
78
|
/**
|
|
75
79
|
* Calls a selector given the current state and extra arguments.
|
|
76
80
|
*
|
|
77
|
-
* @param {string|
|
|
78
|
-
*
|
|
81
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
82
|
+
* or the store descriptor.
|
|
79
83
|
*
|
|
80
84
|
* @return {*} The selector's returned value.
|
|
81
85
|
*/
|
|
82
|
-
function select(
|
|
83
|
-
const storeName = isObject(
|
|
84
|
-
?
|
|
85
|
-
:
|
|
86
|
+
function select( storeNameOrDescriptor ) {
|
|
87
|
+
const storeName = isObject( storeNameOrDescriptor )
|
|
88
|
+
? storeNameOrDescriptor.name
|
|
89
|
+
: storeNameOrDescriptor;
|
|
86
90
|
__experimentalListeningStores.add( storeName );
|
|
87
91
|
const store = stores[ storeName ];
|
|
88
92
|
if ( store ) {
|
|
@@ -105,15 +109,15 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
105
109
|
* and modified so that they return promises that resolve to their eventual values,
|
|
106
110
|
* after any resolvers have ran.
|
|
107
111
|
*
|
|
108
|
-
* @param {string|
|
|
109
|
-
*
|
|
112
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
113
|
+
* or the store descriptor.
|
|
110
114
|
*
|
|
111
115
|
* @return {Object} Each key of the object matches the name of a selector.
|
|
112
116
|
*/
|
|
113
|
-
function resolveSelect(
|
|
114
|
-
const storeName = isObject(
|
|
115
|
-
?
|
|
116
|
-
:
|
|
117
|
+
function resolveSelect( storeNameOrDescriptor ) {
|
|
118
|
+
const storeName = isObject( storeNameOrDescriptor )
|
|
119
|
+
? storeNameOrDescriptor.name
|
|
120
|
+
: storeNameOrDescriptor;
|
|
117
121
|
__experimentalListeningStores.add( storeName );
|
|
118
122
|
const store = stores[ storeName ];
|
|
119
123
|
if ( store ) {
|
|
@@ -126,15 +130,15 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
126
130
|
/**
|
|
127
131
|
* Returns the available actions for a part of the state.
|
|
128
132
|
*
|
|
129
|
-
* @param {string|
|
|
130
|
-
*
|
|
133
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
134
|
+
* or the store descriptor.
|
|
131
135
|
*
|
|
132
136
|
* @return {*} The action's returned value.
|
|
133
137
|
*/
|
|
134
|
-
function dispatch(
|
|
135
|
-
const storeName = isObject(
|
|
136
|
-
?
|
|
137
|
-
:
|
|
138
|
+
function dispatch( storeNameOrDescriptor ) {
|
|
139
|
+
const storeName = isObject( storeNameOrDescriptor )
|
|
140
|
+
? storeNameOrDescriptor.name
|
|
141
|
+
: storeNameOrDescriptor;
|
|
138
142
|
const store = stores[ storeName ];
|
|
139
143
|
if ( store ) {
|
|
140
144
|
return store.getActions();
|
|
@@ -159,56 +163,80 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
159
163
|
}
|
|
160
164
|
|
|
161
165
|
/**
|
|
162
|
-
* Registers a
|
|
166
|
+
* Registers a store instance.
|
|
163
167
|
*
|
|
164
|
-
* @param {string}
|
|
165
|
-
* @param {Object}
|
|
168
|
+
* @param {string} name Store registry name.
|
|
169
|
+
* @param {Object} store Store instance object (getSelectors, getActions, subscribe).
|
|
166
170
|
*/
|
|
167
|
-
function
|
|
168
|
-
if ( typeof
|
|
169
|
-
throw new TypeError( '
|
|
171
|
+
function registerStoreInstance( name, store ) {
|
|
172
|
+
if ( typeof store.getSelectors !== 'function' ) {
|
|
173
|
+
throw new TypeError( 'store.getSelectors must be a function' );
|
|
170
174
|
}
|
|
171
|
-
if ( typeof
|
|
172
|
-
throw new TypeError( '
|
|
175
|
+
if ( typeof store.getActions !== 'function' ) {
|
|
176
|
+
throw new TypeError( 'store.getActions must be a function' );
|
|
173
177
|
}
|
|
174
|
-
if ( typeof
|
|
175
|
-
throw new TypeError( '
|
|
178
|
+
if ( typeof store.subscribe !== 'function' ) {
|
|
179
|
+
throw new TypeError( 'store.subscribe must be a function' );
|
|
176
180
|
}
|
|
177
|
-
//
|
|
181
|
+
// The emitter is used to keep track of active listeners when the registry
|
|
178
182
|
// get paused, that way, when resumed we should be able to call all these
|
|
179
183
|
// pending listeners.
|
|
180
|
-
|
|
181
|
-
const currentSubscribe =
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if ( config.emitter.isPaused ) {
|
|
188
|
-
config.emitter.emit();
|
|
184
|
+
store.emitter = createEmitter();
|
|
185
|
+
const currentSubscribe = store.subscribe;
|
|
186
|
+
store.subscribe = ( listener ) => {
|
|
187
|
+
const unsubscribeFromEmitter = store.emitter.subscribe( listener );
|
|
188
|
+
const unsubscribeFromStore = currentSubscribe( () => {
|
|
189
|
+
if ( store.emitter.isPaused ) {
|
|
190
|
+
store.emitter.emit();
|
|
189
191
|
return;
|
|
190
192
|
}
|
|
191
193
|
listener();
|
|
192
194
|
} );
|
|
193
195
|
|
|
194
196
|
return () => {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
unsubscribeFromStoreEmitter();
|
|
197
|
+
unsubscribeFromStore?.();
|
|
198
|
+
unsubscribeFromEmitter?.();
|
|
199
199
|
};
|
|
200
200
|
};
|
|
201
|
-
stores[
|
|
202
|
-
|
|
201
|
+
stores[ name ] = store;
|
|
202
|
+
store.subscribe( globalListener );
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
/**
|
|
206
|
-
* Registers a new store
|
|
206
|
+
* Registers a new store given a store descriptor.
|
|
207
207
|
*
|
|
208
|
-
* @param {
|
|
208
|
+
* @param {StoreDescriptor} store Store descriptor.
|
|
209
209
|
*/
|
|
210
210
|
function register( store ) {
|
|
211
|
-
|
|
211
|
+
registerStoreInstance( store.name, store.instantiate( registry ) );
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function registerGenericStore( name, store ) {
|
|
215
|
+
deprecated( 'wp.data.registerGenericStore', {
|
|
216
|
+
since: '5.9',
|
|
217
|
+
alternative: 'wp.data.register( storeDescriptor )',
|
|
218
|
+
} );
|
|
219
|
+
registerStoreInstance( name, store );
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Registers a standard `@wordpress/data` store.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} storeName Unique namespace identifier.
|
|
226
|
+
* @param {Object} options Store description (reducer, actions, selectors, resolvers).
|
|
227
|
+
*
|
|
228
|
+
* @return {Object} Registered store object.
|
|
229
|
+
*/
|
|
230
|
+
function registerStore( storeName, options ) {
|
|
231
|
+
if ( ! options.reducer ) {
|
|
232
|
+
throw new TypeError( 'Must specify store reducer' );
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const store = createReduxStore( storeName, options ).instantiate(
|
|
236
|
+
registry
|
|
237
|
+
);
|
|
238
|
+
registerStoreInstance( storeName, store );
|
|
239
|
+
return store.store;
|
|
212
240
|
}
|
|
213
241
|
|
|
214
242
|
/**
|
|
@@ -244,7 +272,6 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
244
272
|
|
|
245
273
|
let registry = {
|
|
246
274
|
batch,
|
|
247
|
-
registerGenericStore,
|
|
248
275
|
stores,
|
|
249
276
|
namespaces: stores, // TODO: Deprecate/remove this.
|
|
250
277
|
subscribe,
|
|
@@ -253,35 +280,21 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
253
280
|
dispatch,
|
|
254
281
|
use,
|
|
255
282
|
register,
|
|
283
|
+
registerGenericStore,
|
|
284
|
+
registerStore,
|
|
256
285
|
__experimentalMarkListeningStores,
|
|
257
286
|
__experimentalSubscribeStore,
|
|
258
287
|
};
|
|
259
288
|
|
|
260
|
-
/**
|
|
261
|
-
* Registers a standard `@wordpress/data` store.
|
|
262
|
-
*
|
|
263
|
-
* @param {string} storeName Unique namespace identifier.
|
|
264
|
-
* @param {Object} options Store description (reducer, actions, selectors, resolvers).
|
|
265
|
-
*
|
|
266
|
-
* @return {Object} Registered store object.
|
|
267
|
-
*/
|
|
268
|
-
registry.registerStore = ( storeName, options ) => {
|
|
269
|
-
if ( ! options.reducer ) {
|
|
270
|
-
throw new TypeError( 'Must specify store reducer' );
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const store = createReduxStore( storeName, options ).instantiate(
|
|
274
|
-
registry
|
|
275
|
-
);
|
|
276
|
-
registerGenericStore( storeName, store );
|
|
277
|
-
return store.store;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
289
|
//
|
|
281
290
|
// TODO:
|
|
282
291
|
// This function will be deprecated as soon as it is no longer internally referenced.
|
|
283
292
|
//
|
|
284
293
|
function use( plugin, options ) {
|
|
294
|
+
if ( ! plugin ) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
285
298
|
registry = {
|
|
286
299
|
...registry,
|
|
287
300
|
...plugin( registry, options ),
|
|
@@ -290,11 +303,11 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
|
|
|
290
303
|
return registry;
|
|
291
304
|
}
|
|
292
305
|
|
|
293
|
-
|
|
306
|
+
registry.register( coreDataStore );
|
|
294
307
|
|
|
295
|
-
|
|
296
|
-
registry.
|
|
297
|
-
|
|
308
|
+
for ( const [ name, config ] of Object.entries( storeConfigs ) ) {
|
|
309
|
+
registry.register( createReduxStore( name, config ) );
|
|
310
|
+
}
|
|
298
311
|
|
|
299
312
|
if ( parent ) {
|
|
300
313
|
parent.subscribe( globalListener );
|
|
@@ -6,7 +6,7 @@ import { get } from 'lodash';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import
|
|
9
|
+
import coreDataStore from './store';
|
|
10
10
|
|
|
11
11
|
/** @typedef {import('./registry').WPDataRegistry} WPDataRegistry */
|
|
12
12
|
|
|
@@ -24,7 +24,7 @@ const createResolversCacheMiddleware = ( registry, reducerKey ) => () => (
|
|
|
24
24
|
next
|
|
25
25
|
) => ( action ) => {
|
|
26
26
|
const resolvers = registry
|
|
27
|
-
.select(
|
|
27
|
+
.select( coreDataStore )
|
|
28
28
|
.getCachedResolvers( reducerKey );
|
|
29
29
|
Object.entries( resolvers ).forEach(
|
|
30
30
|
( [ selectorName, resolversByArgs ] ) => {
|
|
@@ -49,7 +49,7 @@ const createResolversCacheMiddleware = ( registry, reducerKey ) => () => (
|
|
|
49
49
|
|
|
50
50
|
// Trigger cache invalidation
|
|
51
51
|
registry
|
|
52
|
-
.dispatch(
|
|
52
|
+
.dispatch( coreDataStore )
|
|
53
53
|
.invalidateResolution( reducerKey, selectorName, args );
|
|
54
54
|
} );
|
|
55
55
|
}
|
package/src/store/index.js
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const coreDataStore = {
|
|
2
|
+
name: 'core/data',
|
|
3
|
+
instantiate( registry ) {
|
|
4
|
+
const getCoreDataSelector = ( selectorName ) => ( key, ...args ) => {
|
|
5
|
+
return registry.select( key )[ selectorName ]( ...args );
|
|
6
|
+
};
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const getCoreDataAction = ( actionName ) => ( key, ...args ) => {
|
|
9
|
+
return registry.dispatch( key )[ actionName ]( ...args );
|
|
10
|
+
};
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
12
|
+
return {
|
|
13
|
+
getSelectors() {
|
|
14
|
+
return Object.fromEntries(
|
|
15
|
+
[
|
|
16
|
+
'getIsResolving',
|
|
17
|
+
'hasStartedResolution',
|
|
18
|
+
'hasFinishedResolution',
|
|
19
|
+
'isResolving',
|
|
20
|
+
'getCachedResolvers',
|
|
21
|
+
].map( ( selectorName ) => [
|
|
22
|
+
selectorName,
|
|
23
|
+
getCoreDataSelector( selectorName ),
|
|
24
|
+
] )
|
|
25
|
+
);
|
|
26
|
+
},
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
28
|
+
getActions() {
|
|
29
|
+
return Object.fromEntries(
|
|
30
|
+
[
|
|
31
|
+
'startResolution',
|
|
32
|
+
'finishResolution',
|
|
33
|
+
'invalidateResolution',
|
|
34
|
+
'invalidateResolutionForStore',
|
|
35
|
+
'invalidateResolutionForStoreSelector',
|
|
36
|
+
].map( ( actionName ) => [
|
|
37
|
+
actionName,
|
|
38
|
+
getCoreDataAction( actionName ),
|
|
39
|
+
] )
|
|
40
|
+
);
|
|
41
|
+
},
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
43
|
+
subscribe() {
|
|
44
|
+
// There's no reasons to trigger any listener when we subscribe to this store
|
|
45
|
+
// because there's no state stored in this store that need to retrigger selectors
|
|
46
|
+
// if a change happens, the corresponding store where the tracking stated live
|
|
47
|
+
// would have already triggered a "subscribe" call.
|
|
48
|
+
return () => () => {};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
};
|
|
52
53
|
|
|
53
|
-
export default
|
|
54
|
+
export default coreDataStore;
|
package/src/test/registry.js
CHANGED
|
@@ -9,9 +9,9 @@ import { castArray, mapValues } from 'lodash';
|
|
|
9
9
|
import { createRegistry } from '../registry';
|
|
10
10
|
import { createRegistrySelector } from '../factory';
|
|
11
11
|
import createReduxStore from '../redux-store';
|
|
12
|
-
import
|
|
12
|
+
import coreDataStore from '../store';
|
|
13
13
|
|
|
14
|
-
jest.useFakeTimers();
|
|
14
|
+
jest.useFakeTimers( 'legacy' );
|
|
15
15
|
|
|
16
16
|
describe( 'createRegistry', () => {
|
|
17
17
|
let registry;
|
|
@@ -72,6 +72,7 @@ describe( 'createRegistry', () => {
|
|
|
72
72
|
subscribe,
|
|
73
73
|
} )
|
|
74
74
|
).toThrow();
|
|
75
|
+
expect( console ).toHaveWarned();
|
|
75
76
|
} );
|
|
76
77
|
|
|
77
78
|
describe( 'getSelectors', () => {
|
|
@@ -276,7 +277,7 @@ describe( 'createRegistry', () => {
|
|
|
276
277
|
expect( resolver ).toHaveBeenCalledTimes( 2 );
|
|
277
278
|
} );
|
|
278
279
|
|
|
279
|
-
it( 'should support the object resolver
|
|
280
|
+
it( 'should support the object resolver descriptor', () => {
|
|
280
281
|
const resolver = jest.fn();
|
|
281
282
|
registry.registerStore( 'demo', {
|
|
282
283
|
reducer: ( state = 'OK' ) => state,
|
|
@@ -377,7 +378,7 @@ describe( 'createRegistry', () => {
|
|
|
377
378
|
() => registry.select( 'demo' ).getValue() === 'OK',
|
|
378
379
|
() =>
|
|
379
380
|
registry
|
|
380
|
-
.select(
|
|
381
|
+
.select( coreDataStore )
|
|
381
382
|
.hasFinishedResolution( 'demo', 'getValue' ),
|
|
382
383
|
] );
|
|
383
384
|
|
|
@@ -404,7 +405,7 @@ describe( 'createRegistry', () => {
|
|
|
404
405
|
() => registry.select( 'demo' ).getValue() === 'OK',
|
|
405
406
|
() =>
|
|
406
407
|
registry
|
|
407
|
-
.select(
|
|
408
|
+
.select( coreDataStore )
|
|
408
409
|
.hasFinishedResolution( 'demo', 'getValue' ),
|
|
409
410
|
] );
|
|
410
411
|
|
|
@@ -414,31 +415,6 @@ describe( 'createRegistry', () => {
|
|
|
414
415
|
return promise;
|
|
415
416
|
} );
|
|
416
417
|
|
|
417
|
-
it( 'should resolve promise non-action to dispatch', () => {
|
|
418
|
-
let shouldThrow = false;
|
|
419
|
-
registry.registerStore( 'demo', {
|
|
420
|
-
reducer: ( state = 'OK' ) => {
|
|
421
|
-
if ( shouldThrow ) {
|
|
422
|
-
throw 'Should not have dispatched';
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
return state;
|
|
426
|
-
},
|
|
427
|
-
selectors: {
|
|
428
|
-
getValue: ( state ) => state,
|
|
429
|
-
},
|
|
430
|
-
resolvers: {
|
|
431
|
-
getValue: () => Promise.resolve(),
|
|
432
|
-
},
|
|
433
|
-
} );
|
|
434
|
-
shouldThrow = true;
|
|
435
|
-
|
|
436
|
-
registry.select( 'demo' ).getValue();
|
|
437
|
-
jest.runAllTimers();
|
|
438
|
-
|
|
439
|
-
return new Promise( ( resolve ) => process.nextTick( resolve ) );
|
|
440
|
-
} );
|
|
441
|
-
|
|
442
418
|
it( 'should not dispatch resolved promise action on subsequent selector calls', () => {
|
|
443
419
|
registry.registerStore( 'demo', {
|
|
444
420
|
reducer: ( state = 'NOTOK', action ) => {
|
|
@@ -508,7 +484,7 @@ describe( 'createRegistry', () => {
|
|
|
508
484
|
} );
|
|
509
485
|
|
|
510
486
|
describe( 'register', () => {
|
|
511
|
-
it( 'should work with the store
|
|
487
|
+
it( 'should work with the store descriptor as param for select', () => {
|
|
512
488
|
const store = createReduxStore( 'demo', {
|
|
513
489
|
reducer: ( state = 'OK' ) => state,
|
|
514
490
|
selectors: {
|
|
@@ -520,7 +496,7 @@ describe( 'createRegistry', () => {
|
|
|
520
496
|
expect( registry.select( store ).getValue() ).toBe( 'OK' );
|
|
521
497
|
} );
|
|
522
498
|
|
|
523
|
-
it( 'should work with the store
|
|
499
|
+
it( 'should work with the store descriptor as param for dispatch', async () => {
|
|
524
500
|
const store = createReduxStore( 'demo', {
|
|
525
501
|
reducer( state = 'OK', action ) {
|
|
526
502
|
if ( action.type === 'UPDATE' ) {
|
|
@@ -807,15 +783,19 @@ describe( 'createRegistry', () => {
|
|
|
807
783
|
const getSelectors = () => ( { mySelector } );
|
|
808
784
|
const getActions = () => ( { myAction } );
|
|
809
785
|
const subscribe = () => {};
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
786
|
+
const myStore = {
|
|
787
|
+
name: 'store',
|
|
788
|
+
instantiate: () => ( {
|
|
789
|
+
getSelectors,
|
|
790
|
+
getActions,
|
|
791
|
+
subscribe,
|
|
792
|
+
} ),
|
|
793
|
+
};
|
|
794
|
+
registry.register( myStore );
|
|
815
795
|
const subRegistry = createRegistry( {}, registry );
|
|
816
796
|
|
|
817
|
-
subRegistry.select(
|
|
818
|
-
subRegistry.dispatch(
|
|
797
|
+
subRegistry.select( myStore ).mySelector();
|
|
798
|
+
subRegistry.dispatch( myStore ).myAction();
|
|
819
799
|
|
|
820
800
|
expect( mySelector ).toHaveBeenCalled();
|
|
821
801
|
expect( myAction ).toHaveBeenCalled();
|
|
@@ -827,10 +807,13 @@ describe( 'createRegistry', () => {
|
|
|
827
807
|
const getSelectors = () => ( { mySelector } );
|
|
828
808
|
const getActions = () => ( { myAction } );
|
|
829
809
|
const subscribe = () => {};
|
|
830
|
-
registry.
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
810
|
+
registry.register( {
|
|
811
|
+
name: 'store',
|
|
812
|
+
instantiate: () => ( {
|
|
813
|
+
getSelectors,
|
|
814
|
+
getActions,
|
|
815
|
+
subscribe,
|
|
816
|
+
} ),
|
|
834
817
|
} );
|
|
835
818
|
|
|
836
819
|
const subRegistry = createRegistry( {}, registry );
|
|
@@ -839,10 +822,13 @@ describe( 'createRegistry', () => {
|
|
|
839
822
|
const getSelectors2 = () => ( { mySelector: mySelector2 } );
|
|
840
823
|
const getActions2 = () => ( { myAction: myAction2 } );
|
|
841
824
|
const subscribe2 = () => {};
|
|
842
|
-
subRegistry.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
825
|
+
subRegistry.register( {
|
|
826
|
+
name: 'store',
|
|
827
|
+
instantiate: () => ( {
|
|
828
|
+
getSelectors: getSelectors2,
|
|
829
|
+
getActions: getActions2,
|
|
830
|
+
subscribe: subscribe2,
|
|
831
|
+
} ),
|
|
846
832
|
} );
|
|
847
833
|
|
|
848
834
|
subRegistry.select( 'store' ).mySelector();
|