Coder Social home page Coder Social logo

create-selector's Introduction

create-selector

No Maintenance Intended

Simple util that wraps reselect's createSelector but adds the ability to define selector dependencies as strings, then resolve them later.

This makes it easier to compose selectors from different parts of your app or combine functionality from different app bundles without having to have direct references to input functions when you're defining them.

In this way you can defer the creation of a selector and populate it later without needing to have direct references to the input selectors when you're first defining it.

Just like reselect, it also attaches the last function as a .resultFunc property for easy testing without needing all the input functions.

example

import { createSelector, resolveSelectors } from 'create-selector'

export const selectUserData = state => state.user
export const shouldFetchData = createSelector(
  'selectIsLoggedIn',
  selectUserData,
  (loggedIn, userData) => {
    if (loggedIn && !userData) {
      return true
    }
  }
)
import { shouldFetchData } from './other-selectors'
import { selectIsLoggedIn } from './auth-selectors'

// later, you can aggregate them
const selectorAggregator = {
  selectIsLoggedIn,
  shouldFetchData
}

// resolves all the string references with the real ones recursively
// until you've got an object with all your selectors combined
resolveSelectors(selectorAggregator)

that's it!

Notes

  • There's some tests to show this does what it's supposed to but most of the actual work happens in reselect.
  • It tolerates mixing in real selectors too (even if they were created with reselect, directly).

install

npm install create-selector

changes

  • 5.0.0 Removed source maps from build. Fixed npm security warnings w/ audit.
  • 4.0.3 Optimizing selector resolution algorithm. Huge thanks to @rudionrails and @layflags.
  • 4.0.1 building with microbundle (should fix issues with module field in package.json)
  • 2.2.0
    • added support for fully resolved input selectors not having to be on the final object
    • improved error handling
    • more test coverage
    • updated dependencies

credits

If you like this follow @HenrikJoreteg on twitter. But in terms of credit, this is just a simple util on top of reselect all the real magic is in there.

license

MIT

create-selector's People

Contributors

henrikjoreteg avatar rudionrails avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

create-selector's Issues

Selector resolve timeout too short

Hi Henrik,

I currently use redux-bundler for a SPA at a big Telco company. The App currently has >920 selectors and lots of bundles. The crazy thing is, that in Chrome on Windows and Linux (Ubuntu) I reach the selector resolve timeout limit. Chrome on MacOS works fine, as well as FF on all systems.

To solve this problem I just shitfixed node_modules/redux-bundler/dist/redux-bundler.m.js by replacing >500 with >1000 on postinstall.

Have you ever experienced this issue in a bigger SPA? Do you have a recommendation on how to avoid reaching the timeout limit? Or would it be ok to just increase the timeout to 1000 msec in the create-selector package and publish a new version of redux-bundler?

Thanks!

if (duration > 500) {

Support for createStructuredSelector too?

Hi there

This is a great package, helps me cracking cyclic dependencies between multiple subscribers.
Do you think you also can wrap the createStructuredSelector and maybe other remaining public imports from reselect?

There is also the createCachedSelector from re-reselect but probably difficult to include?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.