-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
registry.js
253 lines (227 loc) · 6.77 KB
/
registry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* External dependencies
*/
import {
omit,
without,
mapValues,
} from 'lodash';
import memize from 'memize';
/**
* Internal dependencies
*/
import createNamespace from './namespace-store';
import createCoreDataStore from './store';
/**
* @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.
*
* @property {Function} registerGenericStore Given a namespace key and settings
* object, registers a new generic
* store.
* @property {Function} registerStore Given a namespace key and settings
* object, registers a new namespace
* store.
* @property {Function} subscribe Given a function callback, invokes
* the callback on any change to state
* within any registered store.
* @property {Function} select Given a namespace key, returns an
* object of the store's registered
* selectors.
* @property {Function} dispatch Given a namespace key, returns an
* object of the store's registered
* action dispatchers.
*/
/**
* @typedef {Object} WPDataPlugin An object of registry function overrides.
*/
/**
* Creates a new store registry, given an optional object of initial store
* configurations.
*
* @param {Object} storeConfigs Initial store configurations.
* @param {Object?} parent Parent registry.
*
* @return {WPDataRegistry} Data registry.
*/
export function createRegistry( storeConfigs = {}, parent = null ) {
const stores = {};
let listeners = [];
/**
* Global listener called for each store's update.
*/
function globalListener() {
listeners.forEach( ( listener ) => listener() );
}
/**
* Subscribe to changes to any data.
*
* @param {Function} listener Listener function.
*
* @return {Function} Unsubscribe function.
*/
const subscribe = ( listener ) => {
listeners.push( listener );
return () => {
listeners = without( listeners, listener );
};
};
/**
* Calls a selector given the current state and extra arguments.
*
* @param {string} reducerKey Part of the state shape to register the
* selectors for.
*
* @return {*} The selector's returned value.
*/
function select( reducerKey ) {
const store = stores[ reducerKey ];
if ( store ) {
return store.getSelectors();
}
return parent && parent.select( reducerKey );
}
const getResolveSelectors = memize(
( selectors ) => {
return mapValues(
omit(
selectors,
[
'getIsResolving',
'hasStartedResolution',
'hasFinishedResolution',
'isResolving',
'getCachedResolvers',
]
),
( selector, selectorName ) => {
return ( ...args ) => {
return new Promise( ( resolve ) => {
const hasFinished = () => selectors
.hasFinishedResolution( selectorName, args );
const getResult = () => selector.apply( null, args );
// trigger the selector (to trigger the resolver)
const result = getResult();
if ( hasFinished() ) {
return resolve( result );
}
const unsubscribe = subscribe( () => {
if ( hasFinished() ) {
unsubscribe();
resolve( getResult() );
}
} );
} );
};
}
);
},
{ maxSize: 1 }
);
/**
* Given the name of a registered store, returns an object containing the store's
* selectors pre-bound to state so that you only need to supply additional arguments,
* and modified so that they return promises that resolve to their eventual values,
* after any resolvers have ran.
*
* @param {string} reducerKey Part of the state shape to register the
* selectors for.
*
* @return {Object} Each key of the object matches the name of a selector.
*/
function __experimentalResolveSelect( reducerKey ) {
return getResolveSelectors( select( reducerKey ) );
}
/**
* Returns the available actions for a part of the state.
*
* @param {string} reducerKey Part of the state shape to dispatch the
* action for.
*
* @return {*} The action's returned value.
*/
function dispatch( reducerKey ) {
const store = stores[ reducerKey ];
if ( store ) {
return store.getActions();
}
return parent && parent.dispatch( reducerKey );
}
//
// Deprecated
// TODO: Remove this after `use()` is removed.
//
function withPlugins( attributes ) {
return mapValues( attributes, ( attribute, key ) => {
if ( typeof attribute !== 'function' ) {
return attribute;
}
return function() {
return registry[ key ].apply( null, arguments );
};
} );
}
/**
* Registers a generic store.
*
* @param {string} key Store registry key.
* @param {Object} config Configuration (getSelectors, getActions, subscribe).
*/
function registerGenericStore( key, config ) {
if ( typeof config.getSelectors !== 'function' ) {
throw new TypeError( 'config.getSelectors must be a function' );
}
if ( typeof config.getActions !== 'function' ) {
throw new TypeError( 'config.getActions must be a function' );
}
if ( typeof config.subscribe !== 'function' ) {
throw new TypeError( 'config.subscribe must be a function' );
}
stores[ key ] = config;
config.subscribe( globalListener );
}
let registry = {
registerGenericStore,
stores,
namespaces: stores, // TODO: Deprecate/remove this.
subscribe,
select,
__experimentalResolveSelect,
dispatch,
use,
};
/**
* Registers a standard `@wordpress/data` store.
*
* @param {string} reducerKey Reducer key.
* @param {Object} options Store description (reducer, actions, selectors, resolvers).
*
* @return {Object} Registered store object.
*/
registry.registerStore = ( reducerKey, options ) => {
if ( ! options.reducer ) {
throw new TypeError( 'Must specify store reducer' );
}
const namespace = createNamespace( reducerKey, options, registry );
registerGenericStore( reducerKey, namespace );
return namespace.store;
};
//
// TODO:
// This function will be deprecated as soon as it is no longer internally referenced.
//
function use( plugin, options ) {
registry = {
...registry,
...plugin( registry, options ),
};
return registry;
}
registerGenericStore( 'core/data', createCoreDataStore( registry ) );
Object.entries( storeConfigs ).forEach(
( [ name, config ] ) => registry.registerStore( name, config )
);
if ( parent ) {
parent.subscribe( globalListener );
}
return withPlugins( registry );
}