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

redux-ize

v0.0.5

Published

An implementation of the Redux Action Creator Creator pattern

Downloads

4

Readme

Build Status

redux-ize

In redux apps, we have actions, we have action creators, but at Meeshkan, we use Redux Action Creator Creators. Sound like overkill? Maybe. But let me show how we use it and then you be the judge!

So let’s say that we have an action creator that creates an action and, thanks to react-redux, automatically dispatches it through our middleware when we invoke it. The middleware can add all sorts of good stuff (timestamps, device info…), log events, run asynchronous tasks and a whole lot more. Let’s zoom in on our action creator:

export default clapForPost(nClaps) => ({
    type: "CLAP_FOR_POST",
    payload: nClaps
});

Let’s zoom in on our analytics middleware:

import analytics from 'my-analytics-tool'

export default store => next => action => {
  if (action.meta && action.meta.analyticsData) {
    analytics(action.type, action.meta.analyticsData)
  }
  next(action);
}

Hm… in our analytics data, we’d really like to report what page the claps are coming from. So we go back to our action.js file and write a new function:

export default clapForPostWithAnalytics(nClaps, analytics) => ({
   type: "CLAP_FOR_POST",
   payload: nClaps,
   meta: { analytics },
});

Do you smell the code smell yet? What if we also want to introduce navigation data based on where we are in the app? If we have five possible things to add, hello 2⁵ different functions.

What would be nice is if we could add a bit of analytics data to one action, a bit of navigation data to another… So let’s do it with ize!

import { ize } from 'redux-ize';

export const analyticsIze = ize(0)(a => ({
  analytics: a || {},
}));

export const promiseIze = ize(2)((resolve, reject) => ({
  form: {
    resolve,
    reject,
  }
}));

And then in our file with the action creator…

import { promiseIze, analyticsIze } from './ize';
import { Ize } from 'redux-ize';
import { clapForPost } from './actions/self-aggrandizing';

class MyComponent {
  render() {
    const { clapForPost } = this.props;
    const clapper = () =>
      new Promise((res, rej) => clapForPost(res, rej, 3));
    return (<button onClick={clapper}>Hello world</button>);
  }
}

connect(null,
{
  clapForPost: Ize(
    clapForPost,
    promiseIze(),
    analytics({placeInApp: 'MyComponent'})
  ),
})(MyComponent);

And the action that will be sent is:

{
  type: "CLAP_FOR_POST",
  nClaps: 3,
  meta: {
    analytics: {
      placeInApp: 'MyComponent',
    },
    form: {
     resolve: [Function resolve],
     reject: [Function reject],
    },
  },
}

Voilà our action creator creator! Interestingly, this strategy grew from our adaptation of a much-used hack in the redux-form world. Action creator creators are a generalization of this strategy.

At Meeshkan, the rule of thumb has become:

  1. When an action contains information that cannot possibly be linked to the UI, use middleware.
  2. When an action contains only information that is obviously pertinent to the action (i.e. e-mail for login), use an action creator.
  3. When you need to dispatch UI-related information or random top-level hacks without putting them in the action’s payload, the action creator creator pattern is your friend.

Happy hacking!

Meeshkan