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

autoaction

v0.0.11

Published

Declarative data loading for redux + react

Downloads

9

Readme

AutoAction makes data loading declarative

Automatically call redux actions from state.

Setup:

  1. Put @autoaction beneath @connect so it receives new props from Redux
  2. Pass an object to the @autoaction decorator where: i. array keys are action names ii. array values are functions that accept params and state, and return array arguments

How it works

  1. autoaction accepts a map of action-names to a function which returns action arguments.
  2. if any arguments resolve to undefined we don't call that action. This allows actions to update redux state, which then triggers other actions
  3. if actions are called multiple times with the same arguments dedupe and only call these once. This allows child components in a tree to request data that any parents request with ony one request to thee API.

Examples:

// Single argument:
//
// Automatically call getPost with state.router.params.slug, ie:
// getPost(state.router.params.slug)
@autoaction({
  getPost: (params, state) => state.router.params.slug
}, postActions)

// Multiple arguments:
// getPost(params.id, state.router.params.slug)
@autoaction({
  getPost: (params, state) => [params.id, state.router.params.slug]
}, postActions)

// Multiple arguments as object:
// getPost({ id: params.id, slug: state.router.params.slug })
@autoaction({
  getPost: (params, state) => {
    return: {
      id: params.id,
      slug: state.router.params.slug
    };
  }
}, postActions)

// Call an action each time a state/prop value changes but **isn't an action
// argument**
@autoaction({
  // postActions.resetUI will be called with 'post' as the argument each time
  // the 'key' updates (ie. state.router.params.slug changes)
  resetUI: {
    args: 'post',
    key: (params, state) => state.router.params.slug
  },
}, postActions)

And exactly how?

We connect to redux state directly and listen to store changes. We enqueue action calls in componentWillMount for all components and dispatch them in componentDidMount. This allows us to dedupe any action calls from children, allowing all components to request the same actions if need be.

When we receive new props we enqueue actions and dispatch immediately. To prevent stack overflows we delete actions from the queue before dispatching.

API

Basic example

Action:

// Note that this function accepts an object and immediately destructures into
// arguments. It is called via getPostBySlug({ slug: 'some-post' });
export function getPostBySlug({ slug }) {
  return {
    type: "GET_POST",
    meta: {
      promise: Axios.get(`/api/posts/${slug}`)
    }
  };
}

Component:

import autoaction from 'autoaction';
import * as postActions from 'actions/posts';
import { createStructuredSelector } from 'reselect';

const mapState = createStructuredSelector({
  post: (state) => state.post,
  comments: (state) => state.comments[state.post]
});

// In this example, getPostBySlug will be called from redux-router state
// immediately.  `params.post.id` returns undefined and so
// `getCommentsbyPostId` won't be called immediately.
// When getPostBySlug resolves the component will receive post props and will
// call getCommentsByPostID automatically.
@connect(mapState)
@autoaction({
  getPostBySlug: (params, state) => { return { slug: state.router.params.slug }; }
  getCommentsByPostID: (params, state) => params.post.id
}, postActions)
class BlogPost extends Component {
  static propTypes = {
    post: PropTypes.object
  }

  render() {
    return (
      <h1>postActions.getPostBySlug was automatically called with the slug
        router parameter!</h1>
    );
  }
}