npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-redux-selector

v0.1.0

Published

Asynchronous Redux selectors with 'just in time' data fetching.

Downloads

13

Readme

async-redux-selector

npm version

Example

Suppose we have a web application in which the user can search for pets based on some filter. We want to:

  • fetch the pets that match this filter from the server whenever the filter changes,
  • display a loader while fetching

In applications we were used to building this looks like this:

  1. Dispatch an action to persist the new filter to the store;
  2. Dispatch an action to show a loader;
  3. Fetch pets from the server;
  4. Dispatch an action to persist the received pets to the store;
  5. Dispatch an action to hide the loader.

This is a lot of work.

https://paulgray.net/race-conditions-in-redux/

https://medium.com/faceyspacey/redux-first-router-data-fetching-solving-the-80-use-case-for-async-middleware-14529606c262

https://daveceddia.com/where-fetch-data-redux/

https://github.com/kouhin/redux-dataloader

https://github.com/brocoders/redux-async-connect

The downsides of this approach:

It's hard to figure out where to fetch the data.

We might initiate the request from the filter component. Of course, the filter component has to know which components are visible and what data they require (as we don't want to fetch data this isn't going to be used). This isn't such a big issue in the pet store, but might get pretty complicated for larger applications.

We might also choose to fetch the data from the components where that data is actually used. This would require those components to "watch" the filter for changes. Furthermore, we need to take care not to duplicate requests when two components depend on the same piece of data.

Both these solutions couple the components that render the data and the filter component together.

  • The request is initiated from the filter component. This component has to know what data needs to be fetched. For a simple pet store, this might not be problematic, but for complex UI's where different components might

, and we're even ignoring the possible race conditions that might occur when the first search request took a very long time to resolve; so long, that the user didn't wait for the results and changed the filter again, which returned before the first request did.

Cool

  • Program without having to worry about where your data comes from. This makes for easy testing too, as components don't need data fetching mocked, and neither do selectors.
  • Reduce boilerplate.
  • Data requests are cached for a while which prevents duplicated requests.
  • No worries about race conditions.
  • Chains of dependent async selectors will kick of multiple requests in sequence. The component will receives the data when the final piece of data arrives.
  • You can periodically update data by clearing the cache; components will only show a loading indicator when they fetch data because of something the user changed, in all other cases it will show the most recent data from the server.