Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data: Create registry selector with self-contained registry proxying #16692

Merged
merged 4 commits into from Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/data/src/factory.js
@@ -1,3 +1,12 @@
/**
* Internal dependencies
*/
import defaultRegistry from './default-registry';

/**
* @typedef {import('./registry').WPDataRegistry} WPDataRegistry
*/

/**
* Mark a selector as a registry selector.
*
Expand All @@ -6,9 +15,25 @@
* @return {function} marked registry selector.
*/
export function createRegistrySelector( registrySelector ) {
registrySelector.isRegistrySelector = true;
const selector = ( ...args ) => registrySelector( selector.registry.select )( ...args );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me where selector.registry.select is coming from as the argument passed to registrySelector. Is it supposed to be defaultRegistry.select or I'm guessing it will it be whatever is set on selector.registry at the time it's invoked (with a fallback to defaultRegistry)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me where selector.registry.select is coming from as the argument passed to registrySelector. Is it supposed to be defaultRegistry.select or I'm guessing it will it be whatever is set on selector.registry at the time it's invoked (with a fallback to defaultRegistry)?

It happens here:

if ( selector.isRegistrySelector ) {
selector.registry = registry;
}

This occurs when a store is registered to a registry, so effectively the default registry is never used. I added it to simplify / respect the @type declaration (tangentially related to #16693), but it could just as well be a noop stub.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I wonder if using a noop stub instead might lead to less head scratching for future looks at this code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I wonder if using a noop stub instead might lead to less head scratching for future looks at this code?

If we stubbed it, I wouldn't feel comfortable adding @type {WPDataRegistry}, since it'd be wrong to claim that the stub is of that type (and I expect potential future type checking could flag it as such).

I'm leaning that the current comment for selector.registry assignment alone could be clear enough to explain its purpose.

https://github.com/WordPress/gutenberg/blob/a9e45dc3cdaa7696ac5e686a0ff6fe9eec1f9a5b/packages/data/src/factory.js#L25-L26


/**
* Flag indicating to selector registration mapping that the selector should
* be mapped as a registry selector.
*
* @type {boolean}
*/
selector.isRegistrySelector = true;

/**
* Registry on which to call `select`, stubbed for non-standard usage to
* use the default registry.
*
* @type {WPDataRegistry}
*/
selector.registry = defaultRegistry;

return registrySelector;
return selector;
}

/**
Expand Down
26 changes: 8 additions & 18 deletions packages/data/src/namespace-store/index.js
Expand Up @@ -51,16 +51,12 @@ export default function createNamespace( key, options, registry ) {
...mapValues( metadataSelectors, ( selector ) => ( state, ...args ) => selector( state.metadata, ...args ) ),
...mapValues( options.selectors, ( selector ) => {
if ( selector.isRegistrySelector ) {
const mappedSelector = ( reg ) => ( state, ...args ) => {
return selector( reg )( state.root, ...args );
};
mappedSelector.isRegistrySelector = selector.isRegistrySelector;
return mappedSelector;
selector.registry = registry;
}

return ( state, ...args ) => selector( state.root, ...args );
} ),
}, store, registry );
}, store );
if ( options.resolvers ) {
const result = mapResolvers( options.resolvers, selectors, store );
resolvers = result.resolvers;
Expand Down Expand Up @@ -152,21 +148,15 @@ function createReduxStore( key, options, registry ) {
/**
* Maps selectors to a store.
*
* @param {Object} selectors Selectors to register. Keys will be used as
* the public facing API. Selectors will get
* passed the state as first argument.
* @param {Object} store The store to which the selectors should be
* mapped.
* @param {WPDataRegistry} registry Registry reference.
* @param {Object} selectors Selectors to register. Keys will be used as the
* public facing API. Selectors will get passed the
* state as first argument.
* @param {Object} store The store to which the selectors should be mapped.
*
* @return {Object} Selectors mapped to the provided store.
*/
function mapSelectors( selectors, store, registry ) {
const createStateSelector = ( registeredSelector ) => {
const registrySelector = registeredSelector.isRegistrySelector ?
registeredSelector( registry.select ) :
registeredSelector;

function mapSelectors( selectors, store ) {
const createStateSelector = ( registrySelector ) => {
const selector = function runSelector() {
// This function is an optimized implementation of:
//
Expand Down
32 changes: 32 additions & 0 deletions packages/data/src/test/registry.js
Expand Up @@ -464,6 +464,38 @@ describe( 'createRegistry', () => {

expect( registry.select( 'reducer2' ).selector2() ).toEqual( 'result1' );
} );

it( 'should run the registry selector from a non-registry selector', () => {
const selector1 = () => 'result1';
const selector2 = createRegistrySelector( ( select ) => () =>
select( 'reducer1' ).selector1()
);
const selector3 = () => selector2();
registry.registerStore( 'reducer1', {
reducer: () => 'state1',
selectors: {
selector1,
},
} );
registry.registerStore( 'reducer2', {
reducer: () => 'state1',
selectors: {
selector2,
selector3,
},
} );

expect( registry.select( 'reducer2' ).selector3() ).toEqual( 'result1' );
} );

it( 'gracefully stubs select on selector calls', () => {
const selector = createRegistrySelector( ( select ) => () => select );

const maybeSelect = selector();

expect( maybeSelect ).toEqual( expect.any( Function ) );
expect( maybeSelect() ).toEqual( expect.any( Object ) );
} );
} );

describe( 'subscribe', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/store/test/selectors.js
Expand Up @@ -44,7 +44,7 @@ const {
isCurrentPostScheduled,
isEditedPostPublishable,
isEditedPostSaveable,
isEditedPostAutosaveable: isEditedPostAutosaveableRegistrySelector,
isEditedPostAutosaveable: _isEditedPostAutosaveableRegistrySelector,
isEditedPostEmpty,
isEditedPostBeingScheduled,
isEditedPostDateFloating,
Expand All @@ -71,9 +71,14 @@ const {

describe( 'selectors', () => {
let cachedSelectors;
let isEditedPostAutosaveableRegistrySelector;

beforeAll( () => {
cachedSelectors = filter( selectors, ( selector ) => selector.clear );
isEditedPostAutosaveableRegistrySelector = ( select ) => {
_isEditedPostAutosaveableRegistrySelector.registry = { select };
return _isEditedPostAutosaveableRegistrySelector;
};
} );

beforeEach( () => {
Expand Down