|
1 | | -/** |
2 | | - * WordPress dependencies |
3 | | - */ |
4 | | -import { createReduxStore, register } from '@wordpress/data'; |
5 | | - |
6 | | -/** |
7 | | - * Internal dependencies |
8 | | - */ |
9 | | -import reducer from './reducer'; |
10 | | -import * as selectors from './selectors'; |
11 | | -import * as privateSelectors from './private-selectors'; |
12 | | -import * as actions from './actions'; |
13 | | -import * as privateActions from './private-actions'; |
14 | | -import * as resolvers from './resolvers'; |
15 | | -import createLocksActions from './locks/actions'; |
16 | | -import { |
17 | | - rootEntitiesConfig, |
18 | | - additionalEntityConfigLoaders, |
19 | | - getMethodName, |
20 | | -} from './entities'; |
21 | | -import { STORE_NAME } from './name'; |
22 | | -import { unlock } from './lock-unlock'; |
23 | | -import { dynamicActions, dynamicSelectors } from './dynamic-entities'; |
24 | | -import logEntityDeprecation from './utils/log-entity-deprecation'; |
25 | | - |
26 | | -// The entity selectors/resolvers and actions are shortcuts to their generic equivalents |
27 | | -// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords) |
28 | | -// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy... |
29 | | -// The "kind" and the "name" of the entity are combined to generate these shortcuts. |
30 | | -const entitiesConfig = [ |
31 | | - ...rootEntitiesConfig, |
32 | | - ...additionalEntityConfigLoaders.filter( ( config ) => !! config.name ), |
33 | | -]; |
34 | | - |
35 | | -const entitySelectors = entitiesConfig.reduce( ( result, entity ) => { |
36 | | - const { kind, name, plural } = entity; |
37 | | - |
38 | | - const getEntityRecordMethodName = getMethodName( kind, name ); |
39 | | - result[ getEntityRecordMethodName ] = ( state, key, query ) => { |
40 | | - logEntityDeprecation( kind, name, getEntityRecordMethodName, { |
41 | | - isShorthandSelector: true, |
42 | | - alternativeFunctionName: 'getEntityRecord', |
43 | | - } ); |
44 | | - return selectors.getEntityRecord( state, kind, name, key, query ); |
45 | | - }; |
46 | | - |
47 | | - if ( plural ) { |
48 | | - const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' ); |
49 | | - result[ getEntityRecordsMethodName ] = ( state, query ) => { |
50 | | - logEntityDeprecation( kind, name, getEntityRecordsMethodName, { |
51 | | - isShorthandSelector: true, |
52 | | - alternativeFunctionName: 'getEntityRecords', |
53 | | - } ); |
54 | | - return selectors.getEntityRecords( state, kind, name, query ); |
55 | | - }; |
56 | | - } |
57 | | - return result; |
58 | | -}, {} ); |
59 | | - |
60 | | -const entityResolvers = entitiesConfig.reduce( ( result, entity ) => { |
61 | | - const { kind, name, plural } = entity; |
62 | | - const getEntityRecordMethodName = getMethodName( kind, name ); |
63 | | - result[ getEntityRecordMethodName ] = ( key, query ) => { |
64 | | - logEntityDeprecation( kind, name, getEntityRecordMethodName, { |
65 | | - isShorthandSelector: true, |
66 | | - alternativeFunctionName: 'getEntityRecord', |
67 | | - } ); |
68 | | - return resolvers.getEntityRecord( kind, name, key, query ); |
69 | | - }; |
70 | | - |
71 | | - if ( plural ) { |
72 | | - const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' ); |
73 | | - result[ getEntityRecordsMethodName ] = ( ...args ) => { |
74 | | - logEntityDeprecation( kind, plural, getEntityRecordsMethodName, { |
75 | | - isShorthandSelector: true, |
76 | | - alternativeFunctionName: 'getEntityRecords', |
77 | | - } ); |
78 | | - return resolvers.getEntityRecords( kind, name, ...args ); |
79 | | - }; |
80 | | - result[ getEntityRecordsMethodName ].shouldInvalidate = ( action ) => |
81 | | - resolvers.getEntityRecords.shouldInvalidate( action, kind, name ); |
82 | | - } |
83 | | - return result; |
84 | | -}, {} ); |
85 | | - |
86 | | -const entityActions = entitiesConfig.reduce( ( result, entity ) => { |
87 | | - const { kind, name } = entity; |
88 | | - |
89 | | - const saveEntityRecordMethodName = getMethodName( kind, name, 'save' ); |
90 | | - result[ saveEntityRecordMethodName ] = ( record, options ) => { |
91 | | - logEntityDeprecation( kind, name, saveEntityRecordMethodName, { |
92 | | - isShorthandSelector: true, |
93 | | - alternativeFunctionName: 'saveEntityRecord', |
94 | | - } ); |
95 | | - return actions.saveEntityRecord( kind, name, record, options ); |
96 | | - }; |
97 | | - |
98 | | - const deleteEntityRecordMethodName = getMethodName( kind, name, 'delete' ); |
99 | | - result[ deleteEntityRecordMethodName ] = ( key, query, options ) => { |
100 | | - logEntityDeprecation( kind, name, deleteEntityRecordMethodName, { |
101 | | - isShorthandSelector: true, |
102 | | - alternativeFunctionName: 'deleteEntityRecord', |
103 | | - } ); |
104 | | - return actions.deleteEntityRecord( kind, name, key, query, options ); |
105 | | - }; |
106 | | - |
107 | | - return result; |
108 | | -}, {} ); |
109 | | - |
110 | | -const storeConfig = () => ( { |
111 | | - reducer, |
112 | | - actions: { |
113 | | - ...dynamicActions, |
114 | | - ...actions, |
115 | | - ...entityActions, |
116 | | - ...createLocksActions(), |
117 | | - }, |
118 | | - selectors: { |
119 | | - ...dynamicSelectors, |
120 | | - ...selectors, |
121 | | - ...entitySelectors, |
122 | | - }, |
123 | | - resolvers: { ...resolvers, ...entityResolvers }, |
124 | | -} ); |
125 | | - |
126 | | -/** |
127 | | - * Store definition for the code data namespace. |
128 | | - * |
129 | | - * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore |
130 | | - */ |
131 | | -export const store = createReduxStore( STORE_NAME, storeConfig() ); |
132 | | -unlock( store ).registerPrivateSelectors( privateSelectors ); |
133 | | -unlock( store ).registerPrivateActions( privateActions ); |
134 | | -register( store ); // Register store after unlocking private selectors to allow resolvers to use them. |
| 1 | +// Ensure the StoreRegistry module augmentation is included in the declaration |
| 2 | +// output so that consumers get typed access via store name (e.g. select('core')). |
| 3 | +/// <reference path="./store-registry.ts" preserve="true" /> |
| 4 | +export { store } from './store'; |
135 | 5 |
|
136 | 6 | /** |
137 | 7 | * Enums cannot be exported private without losing the ability to narrow types |
|
0 commit comments