Skip to content

Commit

Permalink
Move resolveSelect mapping from registry to store
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Jan 21, 2021
1 parent 59c4e24 commit ffef5a7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
43 changes: 42 additions & 1 deletion packages/data/src/redux-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { createStore, applyMiddleware } from 'redux';
import { flowRight, get, mapValues } from 'lodash';
import { flowRight, get, mapValues, omit } from 'lodash';
import combineReducers from 'turbo-combine-reducers';
import EquivalentKeyMap from 'equivalent-key-map';

Expand Down Expand Up @@ -123,6 +123,8 @@ export default function createReduxStore( key, options ) {

const getSelectors = () => selectors;
const getActions = () => actions;
const getResolveSelectors = () =>
mapResolveSelectors( selectors, store );

// We have some modules monkey-patching the store object
// It's wrong to do so but until we refactor all of our effects to controls
Expand Down Expand Up @@ -156,6 +158,7 @@ export default function createReduxStore( key, options ) {
selectors,
resolvers,
getSelectors,
__experimentalGetResolveSelectors: getResolveSelectors,
getActions,
subscribe,
};
Expand Down Expand Up @@ -266,6 +269,44 @@ function mapActions( actions, store ) {
return mapValues( actions, createBoundAction );
}

/**
* Maps selectors to functions that return a resolution promise for them
*
* @param {Object} selectors Selectors to map.
* @param {Object} store The redux store the selectors select from.
* @return {Object} Selectors mapped to their resolution functions.
*/
function mapResolveSelectors( selectors, store ) {
return mapValues(
omit( selectors, [
'getIsResolving',
'hasStartedResolution',
'hasFinishedResolution',
'isResolving',
'getCachedResolvers',
] ),
( selector, selectorName ) => ( ...args ) =>
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 = store.subscribe( () => {
if ( hasFinished() ) {
unsubscribe();
resolve( getResult() );
}
} );
} )
);
}

/**
* Returns resolvers with matched selectors for a given namespace.
* Resolvers are side effects invoked once per argument set of a given selector call,
Expand Down
55 changes: 11 additions & 44 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* External dependencies
*/
import { omit, without, mapValues, isObject } from 'lodash';
import memize from 'memize';
import { without, mapValues, isObject } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -102,47 +101,6 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
return result;
}

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,
Expand All @@ -155,7 +113,16 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
* @return {Object} Each key of the object matches the name of a selector.
*/
function __experimentalResolveSelect( storeNameOrDefinition ) {
return getResolveSelectors( select( storeNameOrDefinition ) );
const storeName = isObject( storeNameOrDefinition )
? storeNameOrDefinition.name
: storeNameOrDefinition;
__experimentalListeningStores.add( storeName );
const store = stores[ storeName ];
if ( store ) {
return store.__experimentalGetResolveSelectors();
}

return parent && parent.__experimentalResolveSelect( storeName );
}

/**
Expand Down

0 comments on commit ffef5a7

Please sign in to comment.