-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
487 lines (420 loc) · 13.2 KB
/
index.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/**
* External dependencies
*/
import { combineReducers, createStore } from 'redux';
import { flowRight, without, mapValues, overEvery } from 'lodash';
import EquivalentKeyMap from 'equivalent-key-map';
/**
* WordPress dependencies
*/
import { Component, createHigherOrderComponent, pure, compose } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';
/**
* Internal dependencies
*/
export { loadAndPersist, withRehydratation } from './persist';
/**
* Module constants
*/
const stores = {};
const selectors = {};
const actions = {};
let listeners = [];
/**
* Global listener called for each store's update.
*/
export function globalListener() {
listeners.forEach( ( listener ) => listener() );
}
/**
* Convenience for registering reducer with actions and selectors.
*
* @param {string} reducerKey Reducer key.
* @param {Object} options Store description (reducer, actions, selectors, resolvers).
*
* @return {Object} Registered store object.
*/
export function registerStore( reducerKey, options ) {
if ( ! options.reducer ) {
throw new TypeError( 'Must specify store reducer' );
}
const store = registerReducer( reducerKey, options.reducer );
if ( options.actions ) {
registerActions( reducerKey, options.actions );
}
if ( options.selectors ) {
registerSelectors( reducerKey, options.selectors );
}
if ( options.resolvers ) {
registerResolvers( reducerKey, options.resolvers );
}
return store;
}
/**
* Registers a new sub-reducer to the global state and returns a Redux-like store object.
*
* @param {string} reducerKey Reducer key.
* @param {Object} reducer Reducer function.
*
* @return {Object} Store Object.
*/
export function registerReducer( reducerKey, reducer ) {
const enhancers = [];
if ( window.__REDUX_DEVTOOLS_EXTENSION__ ) {
enhancers.push( window.__REDUX_DEVTOOLS_EXTENSION__( { name: reducerKey, instanceId: reducerKey } ) );
}
const store = createStore( reducer, flowRight( enhancers ) );
stores[ reducerKey ] = store;
// Customize subscribe behavior to call listeners only on effective change,
// not on every dispatch.
let lastState = store.getState();
store.subscribe( () => {
const state = store.getState();
const hasChanged = state !== lastState;
lastState = state;
if ( hasChanged ) {
globalListener();
}
} );
return store;
}
/**
* The combineReducers helper function turns an object whose values are different
* reducing functions into a single reducing function you can pass to registerReducer.
*
* @param {Object} reducers An object whose values correspond to different reducing
* functions that need to be combined into one.
*
* @return {Function} A reducer that invokes every reducer inside the reducers
* object, and constructs a state object with the same shape.
*/
export { combineReducers };
/**
* Registers selectors for external usage.
*
* @param {string} reducerKey Part of the state shape to register the
* selectors for.
* @param {Object} newSelectors Selectors to register. Keys will be used as the
* public facing API. Selectors will get passed the
* state as first argument.
*/
export function registerSelectors( reducerKey, newSelectors ) {
const store = stores[ reducerKey ];
const createStateSelector = ( selector ) => ( ...args ) => selector( store.getState(), ...args );
selectors[ reducerKey ] = mapValues( newSelectors, createStateSelector );
}
/**
* Registers resolvers for a given reducer key. Resolvers are side effects
* invoked once per argument set of a given selector call, used in ensuring
* that the data needs for the selector are satisfied.
*
* @param {string} reducerKey Part of the state shape to register the
* resolvers for.
* @param {Object} newResolvers Resolvers to register.
*/
export function registerResolvers( reducerKey, newResolvers ) {
const createResolver = ( selector, key ) => {
// Don't modify selector behavior if no resolver exists.
if ( ! newResolvers.hasOwnProperty( key ) ) {
return selector;
}
const store = stores[ reducerKey ];
// Normalize resolver shape to object.
let resolver = newResolvers[ key ];
if ( ! resolver.fulfill ) {
resolver = { fulfill: resolver };
}
/**
* To ensure that fulfillment occurs only once per arguments set
* (even for deeply "equivalent" arguments), track calls.
*
* @type {EquivalentKeyMap}
*/
const fulfilledByEquivalentArgs = new EquivalentKeyMap();
/**
* Returns true if resolver fulfillment has already occurred for an
* equivalent set of arguments. Includes side effect when returning
* false to ensure the next invocation returns true.
*
* @param {Array} args Arguments set.
*
* @return {boolean} Whether fulfillment has already occurred.
*/
function hasBeenFulfilled( args ) {
const hasArguments = fulfilledByEquivalentArgs.has( args );
if ( ! hasArguments ) {
fulfilledByEquivalentArgs.set( args, true );
}
return hasArguments;
}
async function fulfill( ...args ) {
if ( hasBeenFulfilled( args ) ) {
return;
}
// At this point, selectors have already been pre-bound to inject
// state, it would not be otherwise provided to fulfill.
const state = store.getState();
let fulfillment = resolver.fulfill( state, ...args );
// Attempt to normalize fulfillment as async iterable.
fulfillment = toAsyncIterable( fulfillment );
if ( ! isAsyncIterable( fulfillment ) ) {
return;
}
for await ( const maybeAction of fulfillment ) {
// Dispatch if it quacks like an action.
if ( isActionLike( maybeAction ) ) {
store.dispatch( maybeAction );
}
}
}
if ( typeof resolver.isFulfilled === 'function' ) {
// When resolver provides its own fulfillment condition, fulfill
// should only occur if not already fulfilled (opt-out condition).
fulfill = overEvery( [
( ...args ) => {
const state = store.getState();
return ! resolver.isFulfilled( state, ...args );
},
fulfill,
] );
}
return ( ...args ) => {
fulfill( ...args );
return selector( ...args );
};
};
selectors[ reducerKey ] = mapValues( selectors[ reducerKey ], createResolver );
}
/**
* Registers actions for external usage.
*
* @param {string} reducerKey Part of the state shape to register the
* selectors for.
* @param {Object} newActions Actions to register.
*/
export function registerActions( reducerKey, newActions ) {
const store = stores[ reducerKey ];
const createBoundAction = ( action ) => ( ...args ) => store.dispatch( action( ...args ) );
actions[ reducerKey ] = mapValues( newActions, createBoundAction );
}
/**
* Subscribe to changes to any data.
*
* @param {Function} listener Listener function.
*
* @return {Function} Unsubscribe function.
*/
export 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.
*/
export function select( reducerKey ) {
return selectors[ 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.
*/
export function dispatch( reducerKey ) {
return actions[ reducerKey ];
}
/**
* Higher-order component used to inject state-derived props using registered
* selectors.
*
* @param {Function} mapStateToProps Function called on every state change,
* expected to return object of props to
* merge with the component's own props.
*
* @return {Component} Enhanced component with merged state data props.
*/
export const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( WrappedComponent ) => {
return class ComponentWithSelect extends Component {
constructor() {
super( ...arguments );
this.runSelection = this.runSelection.bind( this );
/**
* Boolean tracking known render conditions (own props or merged
* props update) for `shouldComponentUpdate`.
*
* @type {boolean}
*/
this.shouldComponentUpdate = false;
this.state = {};
}
shouldComponentUpdate() {
return this.shouldComponentUpdate;
}
componentWillMount() {
this.subscribe();
// Populate initial state.
this.runSelection();
}
componentWillReceiveProps( nextProps ) {
if ( ! isShallowEqual( nextProps, this.props ) ) {
this.runSelection( nextProps );
this.shouldComponentUpdate = true;
}
}
componentWillUnmount() {
this.unsubscribe();
// While above unsubscribe avoids future listener calls, callbacks
// are snapshotted before being invoked, so if unmounting occurs
// during a previous callback, we need to explicitly track and
// avoid the `runSelection` that is scheduled to occur.
this.isUnmounting = true;
}
subscribe() {
this.unsubscribe = subscribe( this.runSelection );
}
runSelection( props = this.props ) {
if ( this.isUnmounting ) {
return;
}
const { mergeProps } = this.state;
const nextMergeProps = mapStateToProps( select, props ) || {};
if ( ! isShallowEqual( nextMergeProps, mergeProps ) ) {
this.setState( {
mergeProps: nextMergeProps,
} );
this.shouldComponentUpdate = true;
}
}
render() {
this.shouldComponentUpdate = false;
return <WrappedComponent { ...this.props } { ...this.state.mergeProps } />;
}
};
}, 'withSelect' );
/**
* Higher-order component used to add dispatch props using registered action
* creators.
*
* @param {Object} mapDispatchToProps Object of prop names where value is a
* dispatch-bound action creator, or a
* function to be called with with the
* component's props and returning an
* action creator.
*
* @return {Component} Enhanced component with merged dispatcher props.
*/
export const withDispatch = ( mapDispatchToProps ) => createHigherOrderComponent(
compose( [
pure,
( WrappedComponent ) => {
return class ComponentWithDispatch extends Component {
constructor() {
super( ...arguments );
this.proxyProps = {};
}
componentWillMount() {
this.setProxyProps( this.props );
}
componentWillUpdate( nextProps ) {
this.setProxyProps( nextProps );
}
proxyDispatch( propName, ...args ) {
// Original dispatcher is a pre-bound (dispatching) action creator.
mapDispatchToProps( dispatch, this.props )[ propName ]( ...args );
}
setProxyProps( props ) {
// Assign as instance property so that in reconciling subsequent
// renders, the assigned prop values are referentially equal.
const propsToDispatchers = mapDispatchToProps( dispatch, props );
this.proxyProps = mapValues( propsToDispatchers, ( dispatcher, propName ) => {
// Prebind with prop name so we have reference to the original
// dispatcher to invoke. Track between re-renders to avoid
// creating new function references every render.
if ( this.proxyProps.hasOwnProperty( propName ) ) {
return this.proxyProps[ propName ];
}
return this.proxyDispatch.bind( this, propName );
} );
}
render() {
return <WrappedComponent { ...this.props } { ...this.proxyProps } />;
}
};
},
] ),
'withDispatch'
);
/**
* Returns true if the given argument appears to be a dispatchable action.
*
* @param {*} action Object to test.
*
* @return {boolean} Whether object is action-like.
*/
export function isActionLike( action ) {
return (
!! action &&
typeof action.type === 'string'
);
}
/**
* Returns true if the given object is an async iterable, or false otherwise.
*
* @param {*} object Object to test.
*
* @return {boolean} Whether object is an async iterable.
*/
export function isAsyncIterable( object ) {
return (
!! object &&
typeof object[ Symbol.asyncIterator ] === 'function'
);
}
/**
* Returns true if the given object is iterable, or false otherwise.
*
* @param {*} object Object to test.
*
* @return {boolean} Whether object is iterable.
*/
export function isIterable( object ) {
return (
!! object &&
typeof object[ Symbol.iterator ] === 'function'
);
}
/**
* Normalizes the given object argument to an async iterable, asynchronously
* yielding on a singular or array of generator yields or promise resolution.
*
* @param {*} object Object to normalize.
*
* @return {AsyncGenerator} Async iterable actions.
*/
export function toAsyncIterable( object ) {
if ( isAsyncIterable( object ) ) {
return object;
}
return ( async function* () {
// Normalize as iterable...
if ( ! isIterable( object ) ) {
object = [ object ];
}
for ( let maybeAction of object ) {
// ...of Promises.
if ( ! ( maybeAction instanceof Promise ) ) {
maybeAction = Promise.resolve( maybeAction );
}
yield await maybeAction;
}
}() );
}