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

rehash

v0.6.0

Published

Some utilities for use with redux

Downloads

116

Readme

rehash

A set of utilities to generate an initial state, a reducer, and an action creator tree for use with redux.

Usage

npm install rehash
import { createStore } from 'redux';
import rehash from 'rehash';

const storeDef = {
  items: ['foo'],
  addItem: (state, item) => Object.assign({}, state, {
    items: state.items.concat(item)
  }),
  nested: {
    items: ['bar'],
    addItem: (state, item) => Object.assign({}, state, {
      items: state.items.concat(item)
    })
  }
};

const { state, reducer, getDispatchTree } = rehash(storeDef);
const store = createStore(reducer, state);
const dispatchTree = getDispatchTree(store.dispatch);

store.dispatch({
  type: 'nested.addItem',
  payload: 'baz'
});

console.log(store.getState().nested.items); // ['bar', 'baz']

dispatchTree.nested.addItem('qux');

console.log(store.getState().nested.items); // ['bar', 'baz', 'qux']

Concepts

At its simplest, rehash is a utility to create a redux reducer from a rehash store definition that looks like this:

const storeDef = {
  items: ['foo'],
  addItem: (state, item) => Object.assign({}, state, {
    items: state.items.concat(item)
  }),
  nested: {
    items: ['bar'],
    addItem: (state, item) => Object.assign({}, state, {
      items: state.items.concat(item)
    })
  }
};

Note: A store definition mixes both the initial state and the state mutation functions (or xforms) into a single object so you can easily see the shape of the state that each xform is operating on.

Reducer

Rehash takes this store definition and returns an initial state, and a redux reducer.

const { state, reducer } = rehash(storeDef);

These can be used like any other handwritten reducer in redux. The handwritten equivalent to the above would look like this:

const reducer = (state, action) => {
  switch (action.type) {
    case 'addItem':
      return Object.assign({}, state, {
        items: state.items.concat(action.payload)
      });
    case 'nested.addItem':
      return Object.assign({}, state, {
        nested: Object.assign({}, state.nested, {
          items: state.items.concat(action.payload)
        })
      });
    default:
      return state;
  }
};

Store/dispatch

Use state and reducer with redux:

const store = createStore(reducer, state);

You can now dispatch actions with two properties, type, which is generated from the "path" to the xform in the store definition, and payload, which is given to the xform as the second argument.

store.dispatch({
  type: 'nested.addItem',
  payload: 'foo'
});

DispatchTree

Rehash also returns a function getDispatchTree which, after store creation, you can call with the dispatch function to get a dispatchTree.

const { state, reducer, getDispatchTree } = rehash(storeDef);
const store = createStore(reducer, state);
const dispatchTree = getDispatchTree(store.dispatch);

This is a simple object that mirrors the shape of the xform tree to provide a little bit of sugar over calling store.dispatch directly. The following are equivalent:

store.dispatch({
  type: 'nested.addItem',
  payload: 'foo'
});

dispatchTree.nested.addItem('foo');

Thunks

Because rehash returns a simple reducer, integration with other redux libraries is seamless.

import { applyMiddleware, createStore, compose } from 'redux';
import rehash from 'rehash';
import thunk from 'redux-thunk';

const storeDef = {...};

const { state, reducer, getDispatchTree } = rehash(storeDef);
const store = compose(
  applyMiddleware(thunk)
)(createStore)(reducer, state);
const dispatchTree = getDispatchTree(store.dispatch);

const thunkActionCreator = payload => (dispatch, getState) => {
  setTimeout(() => dispatch({
    type: 'nested.addItem',
    payload: 'foo'
  }), 1000);

  // OR

  setTimeout(() => dispatchTree.nested.addItem('foo'), 1000);
});

You can also use some utilities that rehash provides to include thunk action creators into your store definition:

import rehash, { async, isThunk, thunkCreateActionCreator } from 'rehash';
import { applyMiddleware, createStore, compose } from 'redux';

const storeDef = {
  items: ['foo'],
  addItem: (state, item) => Object.assign({}, state, {
    items: state.items.concat(item)
  }),
  nested: {
    items: ['bar'],
    addItem: (state, item) => Object.assign({}, state, {
      items: state.items.concat(item)
    })
  },
  // wrap the thunk xform so rehash knows what to do with it
  thunkActionCreator: async(payload => (dispatchTree, getState, dispatch) => {
    setTimeout(dispatchTree.addItem(payload), 1000);
  })
};

const opts = {
  // a rehash-provided config function to interpret thunks in your store def
  createActionCreator: thunkCreateActionCreator,
  // tells rehash to ignore thunk xforms when creating the reducer
  reducerTreeFilterFn: isThunk
};

const { state, reducer, getDispatchTree } = rehash(storeDef, opts);
const store = compose(
  applyMiddleware(thunk)
)(createStore)(reducer, state);
const dispatchTree = getDispatchTree(store.dispatch);

// thunks can now be dispatched directly from the dispatchTree
dispatchTree.thunkActionCreator('bar');

To see a full example using redux.combineReducers, redux-thunk, and redux-router with rehash, check out src/example.jsx.

Advanced

@todo: document custom createActionCreator, reducerTreeFilterFn, and actionHandler functions.