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-msr

v0.2.0

Published

Merge resource sub reducers wihtout slicing the Redux state

Downloads

14

Readme

Redux MSR

Merge sub-reducers (MSR) while sharing the same Redux store (or sub-state component), enabling scalable reducer code management and reuse.

Table of Contents

The Problem

Redux's API provides combineReducers() to help manage and split large or complicated store state into smaller chunks that can then be better split/organised within a project. However, even parts of these broken states -- or non-split stores -- handle multiple action types, and/or can include relatively long/complex reducer logic. Code management or readability can quickly become difficult, and we often have to split our reducers into smaller reducers, but then end up with unnecessary code to load or organise such reducers.

This package aims to help provide a simple API to join and reuse reducers, so that code manageement and complexity become easier to manage and scale.

The Solution

Note: The below example is using ducks for organising action creators, constants and the root resource-reducer.

Taking the following simplified example:

const INCREMENT = 'COUNTER.INCREMENT.VALUE';
const DECREMENT = 'COUNTER.DECREMENT.VALUE';

const defaultState = 0;

export const incrementValue = () => ({
  type: INCREMENT
});

export const decrementValue = () => ({
  type: DECREMENT
});

export default (prevCounterState = defaultState, action) => {
  switch (action.type) {
    case INCREMENT:
      // ...
      // ... other complicated or lengthy code
      // ...
      return prevCounterState + 1;
      case DECREMENT:
      // ...
      // ... other complicated or lengthy code
      // ...
      return prevCounterState - 1;
    // other case(s)
    // .
    // .
    // .
    default:
      return prevCounterState;
  }
};

With redux-msr, this can be simplified to:

const defaultState = 0;

import combineSubReducers from 'redux-msr';
import incrementReducer from './reducers/incrementReducers';
import decrementReducer from './reducers/decrementReducers';

const INCREMENT = 'COUNTER.INCREMENT.VALUE';
const DECREMENT = 'COUNTER.DECREMENT.VALUE';

export const incrementValue = () => ({
  type: INCREMENT
});

export const decrementValue = () => ({
  type: DECREMENT
});

export default combineSubReducers(defaultState, {
  [INCREMENT]: incrementReducer,
  [DECREMENT]: decrementReducer
});

Installation

This module is distributed via npm and should be included as part of your package's dependencies:

npm install --save redux-msr

API

combineSubReducers<T>(defaultState: T, reducerConfig: ReducerConfig) => Reducer<T>

defaultState

The default state to use when the Redux store is initialised

reducerConfig

This can either be an Array or an Object.

When an Array, the array should be an array of Redux reducer functions (type: Reducer). Each reducer in the array will be triggered for all action.type's and will be each passed the previous state in sequential order. The result returned by each reducer will be passed to the subsequent in the array, with the final state returned by the array being passed back to the Redux store.

When an Object, the object key-value pairs should correspond to the action type and corresponding value to return for that action type, respectively. If the return value is a function, then this will be called with the standard Redux reducer parameters, namely Reducer (see Redux typing).

The reducer returned by this module will return the previous state unless a specific action type is noted, and a subsequent value or function is supplied in order to change what the new (immutable) state should be. If you need to overwrite the default response of the resulting reducer, i.e. if no action-types match the currently dispatched action, then simply add the required value/function to the default key within the reducerConfig object. For example,

export default combineSubReducers(defaultState, {
  default: myDefaultReducer
});

returnPrevState<T>(prevState: T) => T

A simply utility that can be used to force a simple short circuit and return the previous state. This can be particularly useful when the reducerConfig is the Object variant, and for a given action.type you would like to return the previous state, e.g. during a dispatched error.

Examples

These are just a small set of examples of what is possible. This package can be used on its own for a single Redux store, or one that has been subdivided into smaller store slices using Redux's combineReducers() API, as mentioned above.

reducerConfig: Object type

1. Return basic primitive types for a given action type:

// reducer.js
import combineSubReducers from 'redux-msr';

// State is a Number
const defaultState = 0;

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': 4,
  'EXAMPLE.ACTION.TYPE.2': 20,
});
// reducer.js
import combineSubReducers from 'redux-msr';

// State is a String
const defaultState = '';

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': 'foo',
  'EXAMPLE.ACTION.TYPE.2': 'bar',
});

2. Use sub-reducer(s):

// reducer.js
import combineSubReducers from 'redux-msr';

import handleType1Reducer from './reducers/reducer1';
import handleType2Reducer from './reducers/reducer2';

// State is a Number
const defaultState = 0;

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': handleType1Reducer,
  'EXAMPLE.ACTION.TYPE.2': handleType2Reducer,
});

Here, both handleType1Reducer and handleType2Reducer have the Reducer type signature (see here).

3. A combination

// reducer.js
import combineSubReducers from 'redux-msr';

import handleType1Reducer from './reducers/reducer1';
import handleType2Reducer from './reducers/reducer2';

// State is a Number
const defaultState = 0;

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': handleType1Reducer,
  'EXAMPLE.ACTION.TYPE.2': handleType2Reducer,
  'EXAMPLE.ACTION.TYPE.3': 10,
});

reducerConfig: Array type

// reducer.js
import combineSubReducers from 'redux-msr';

import reducerA from '/some/other/part/of/codebase';

// Assume our state is a simple number
const defaultState = 0;

// Mocked reducer
const reducerB = (prevState: number = defaultState, action: Action) => {
  switch(action.type){
    case 'foo':
      return 100;

    default: return prevState;
  }
};

/**
 * Remember: the order of the reducers in the array matter!
 *
 * State is passed to the first reducer, here `reducerA`; the state returned
 * from reducerA is then passed to `reducerB`; the state returned from reducerB
 * is then sent back to the Redux store.
 */
const mainReducer = comineSubReducers(defaultState, [
  reducerA,
  reducerB,
]);

export default mainReducer;