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

Framework: Add create-selector library for memoized selectors #3545

Merged
merged 3 commits into from
Feb 25, 2016

Conversation

aduth
Copy link
Contributor

@aduth aduth commented Feb 24, 2016

Related: #3522

This pull request implements a new create-selector library for creating memoized state selectors.

It seeks to be an alternative to reselect, better accommodating our patterns for retrieving values with variable arguments.

See included README.md for more information and usage

Testing instructions:

This is not included anywhere in the application, so should have no impact on existing behavior.

Ensure that included Mocha tests pass by running make test from client/lib/create-selector.

/cc @gwwar , @blowery (for dependent/dependant grammar verification 😉 )

@aduth aduth added Framework [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Feb 24, 2016
* @return {Function} Memoized selector
*/
export default function createSelector( selector, getDependants = ( state ) => state ) {
const memoizedSelector = memoize( selector, ( state, ...args ) => args.join() );
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe in dev mode, yell if any args are objects? can see this being really easy to screw up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe in dev mode, yell if any args are objects? can see this being really easy to screw up.

Good idea. Added in 40ef2de.

@gwwar
Copy link
Contributor

gwwar commented Feb 24, 2016

👍 Looks good to me. We'll need to be a bit careful with making sure we list all dependent state.

@gwwar gwwar mentioned this pull request Feb 24, 2016
@aduth aduth force-pushed the add/redux-create-selector branch from f49571d to d3d4472 Compare February 25, 2016 01:38
@aduth aduth added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Feb 25, 2016
return ( state, ...args ) => args.join();
}

const warn = require( 'lib/warn' );
Copy link
Contributor

Choose a reason for hiding this comment

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

huh. does this actually keep these out of the prod bundle? I thought the analyzer was static...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

huh. does this actually keep these out of the prod bundle? I thought the analyzer was static...

In testing this theory, you're correct that it would be included in the bundle, with the caveat being if Webpack is able to determine assuredly that the require would never be reached, i.e. if I had a module defined as:

var m;
if ( true ) {
    m = require( './a' );
} else {
    m = require( './b' );
}

...then Webpack is smart enough not to include the ./b file in the bundle.

All the more reason I think we ought use DefinePlugin and specify a __DEV__ variable, to be replaced with true or false and such optimizations could take effect.

@aduth
Copy link
Contributor Author

aduth commented Feb 25, 2016

@blowery : Lucky for me, we already define process.env.NODE_ENV using Webpack's DefinePlugin, so I've incorporated it in d851550 to take advantage of bundle optimization.

aduth added a commit that referenced this pull request Feb 25, 2016
Framework: Add `create-selector` library for memoized selectors
@aduth aduth merged commit 80d795d into master Feb 25, 2016
@aduth aduth deleted the add/redux-create-selector branch February 25, 2016 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants